[LintCode] Two Sum

本文解析了在整数数组中寻找两个数使它们的和等于特定目标值的问题。提供了三种解决方案:使用哈希表的两次遍历(O(n)时间和空间复杂度),一次遍历的改进版以及利用排序和双指针技术实现O(n log n)时间复杂度的方法。

Given an array of integers, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are NOT zero-based.

You may assume that each input would have exactly one solution

Example

numbers=[2, 7, 11, 15], target=9

return [1, 2]

Challenge 

Either of the following solutions are acceptable:

  • O(n) Space, O(nlogn) Time
  • O(n) Space, O(n) Time

 

Solution 1. O(n) runtime, O(n) space using hash map, two pass

 1 public class Solution {
 2     public int[] twoSum(int[] numbers, int target) {
 3         int[] result = new int[2];
 4         if(numbers == null || numbers.length <= 1)
 5         {
 6             return result;
 7         }
 8         HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
 9         for(int i = 0; i < numbers.length; i++)
10         {
11             map.put(numbers[i], i);
12         }
13         for(int i = 0; i < numbers.length; i++)
14         {
15             if(map.containsKey(target - numbers[i]))
16             {
17                 result[0] = i + 1;
18                 result[1] = map.get(target - numbers[i]) + 1;
19                 break;
20             }
21         }
22         return result;
23     }
24 }

 

Solution 2. O(n) runtime, O(n) space using hash map, one pass

 1 public class Solution {
 2     public int[] twoSum(int[] numbers, int target) {
 3         HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();       
 4         for(int i = 0; i < numbers.length; i++)
 5         {
 6             if(map.get(numbers[i]) != null)
 7             {
 8                 int[] result = {map.get(numbers[i]) + 1, i + 1};
 9                 return result;
10             }
11             map.put(target - numbers[i], i);
12         }
13         int[] result = {};
14         return result;
15     }
16 }

 

Solution 3.  O(n*logn) runtime, O(n) space using sort and two pointers 

 1 class ArrayEntry
 2 {
 3     protected int val;
 4     protected int index;
 5     public ArrayEntry(int val, int index)
 6     {
 7         this.val = val;
 8         this.index = index;
 9     }
10 }
11 public class Solution {
12     public int[] twoSum(int[] numbers, int target) {
13         int[] result = new int[2];
14         if(numbers == null || numbers.length <= 1)
15         {
16             return result;
17         }
18         ArrayEntry[] entries = new ArrayEntry[numbers.length];
19         for(int i = 0; i < numbers.length; i++)
20         {
21             entries[i] = new ArrayEntry(numbers[i], i);
22         }
23         Arrays.sort(entries, new Comparator<ArrayEntry>()
24             {
25                 public int compare(ArrayEntry a1, ArrayEntry a2)
26                 {
27                     return Integer.compare(a1.val, a2.val);
28                 }
29             }
30         );
31         int i = 0, j = entries.length - 1;
32         while(i < j)
33         {
34             int sum = entries[i].val + entries[j].val;
35             if(sum == target)
36             {
37                 if(entries[i].index < entries[j].index)
38                 {
39                     result[0] = entries[i].index + 1;
40                     result[1] = entries[j].index + 1;
41                 }
42                 else
43                 {
44                     result[0] = entries[j].index + 1;
45                     result[1] = entries[i].index + 1;
46                 }
47                 break;
48             }
49             else if(sum < target)
50             {
51                 i++;
52             }
53             else
54             {
55                 j--;
56             }
57         }
58         return result;
59     }
60 }

 

Related Problems

Two Sum - Input array is sorted 

Two Sum - Difference equals to target 

Two Sum - Less than or equal to target

Two Sum - Data structure design

Two Sum - Unique pairs

Two Sum - Closest to target

Two Sum - Greater than target 

Triangle Count

3Sum Closest

4Sum

3Sum

 

转载于:https://www.cnblogs.com/lz87/p/7203577.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值