西维蜀黍的健身 Blog

🐒 Software engineer | 📷 Photographer | 👹 Urban explorer

【刷题】LinkedList - Leetcode - 92 Reverse Linked List II

Reverse a linked list from position m to n. Do it in one-pass.

Note: 1 ≤ mn ≤ length of list.

Example:

Input: 1->2->3->4->5->NULL, m = 2, n = 4
Output: 1->4->3->2->5->NULL
  ...


【刷题】Sort Array - Lintcode - 548 Intersection of Two Arrays II

Description

Given two arrays, write a function to compute their intersection.

Each element in the result should appear as many times as it shows in both arrays.The result can be in any order.

Example

Example1

Input: 
nums1 = [1, 2, 2, 1], nums2 = [2, 2]
Output: 
[2, 2]

Example2

Input: 
nums1 = [1, 1, 2], nums2 = [1]
Output: 
[1]

Challenge

  • What if the given array is already sorted? How would you optimize your algorithm?
  • What if nums1’s size is small compared to num2’s size? Which algorithm is better?
  • What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?
  ...


【刷题】Sort Array - Lintcode - 547 Intersection of Two Arrays

Leetcode - 349 Intersection of Two Arrays

Description

Given two arrays, write a function to compute their intersection.

Each element in the result must be unique.The order of the results needs to be ascending

Example

Example 1:

Input: nums1 = [1, 2, 2, 1], nums2 = [2, 2], 
Output: [2].

Example 2:

Input: nums1 = [1, 2], nums2 = [2], 
Output: [2].
  ...


【刷题】Sort Array - Lintcode - 839 Merge Two Sorted Interval Lists

Description

Merge two sorted (ascending) lists of interval and return it as a new sorted list. The new sorted list should be made by splicing together the intervals of the two lists and sorted in ascending order.

The intervals in the given list do not overlap.The intervals in different lists may overlap.

Example

Example1

Input: list1 = [(1,2),(3,4)] and list2 = [(2,3),(5,6)]
Output: [(1,4),(5,6)]
Explanation:
(1,2),(2,3),(3,4) --> (1,4)
(5,6) --> (5,6)

Example2

Input: list1 = [(1,2),(3,4)] and list2 = [(4,5),(6,7)]
Output: [(1,2),(3,5),(6,7)]
Explanation:
(1,2) --> (1,2)
(3,4),(4,5) --> (3,5)
(6,7) --> (6,7)
  ...


【刷题】Sort Array - Lintcode - 6 Merge Two Sorted Arrays

Description

Merge two given sorted ascending integer array A and B into a new sorted integer array.

Example

Example 1:

Input:  A=[1], B=[1]
Output: [1,1]	
Explanation:  return array merged.

Example 2:

Input:  A=[1,2,3,4], B=[2,4,5,6]
Output: [1,2,2,3,4,4,5,6]	
Explanation: return array merged.

Challenge

How can you optimize your algorithm if one array is very large and the other is very small?

  ...