forked from gouthampradhan/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoSum.java
More file actions
66 lines (55 loc) · 1.88 KB
/
Copy pathTwoSum.java
File metadata and controls
66 lines (55 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package array;
import java.util.ArrayList;
import java.util.List;
/**
* Created by gouthamvidyapradhan on 11/07/2017.
* Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
Solution: O(n log n). Wrap index and element in a class and sort in increasing order. Do a two pointer sum and compare.
An alternative solution is to use hashing which is a O(n) solution - For each element e check if element (target - e)
is already found in hashset, if yes return their index, else add this to hash-set and continue.
*/
public class TwoSum {
class NumIndex
{
int i, e;
NumIndex(int i, int e){
this.i = i;
this.e = e;
}
}
public static void main(String[] args) {
int[] nums = {3, 2, 4};
int[] ans = new TwoSum().twoSum(nums, 6);
for (int i : ans)
System.out.println(i);
}
public int[] twoSum(int[] nums, int target) {
List<NumIndex> list = new ArrayList<>();
for(int i = 0; i < nums.length; i ++){
NumIndex n = new NumIndex(i, nums[i]);
list.add(n);
}
list.sort((o1, o2) -> Integer.compare(o1.e, o2.e));
int[] ans = new int[2];
for(int i = 0, j = nums.length - 1; i < j; ){
NumIndex numi = list.get(i);
NumIndex numj = list.get(j);
int sum = numi.e + numj.e;
if(sum == target){
ans[0] = numi.i;
ans[1] = numj.i;
return ans;
}
else if(sum > target){
j --;
}
else i++;
}
return ans;
}
}