forked from codedecks-in/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
two-sum.java
29 lines (25 loc) · 830 Bytes
/
two-sum.java
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
/**
* One-pass Hash Table
* we iterate and inserting elements into the table,
* we also look back to check if current element's complement already exists in the table.
* If it exists, we have found a solution and return immediately.
*
* Time complexity : O(n)
* Space complexity : O(n)
*/
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> sumMap = new HashMap<>();
int [] result = new int[2];
for (int i=0; i<nums.length(); i++){
if(!sumMap.isEmpty() && sumMap.containsKey(nums[i])){
result[0] = sumMap.get(nums[i]);
result[1] = i;
return result;
}
else{
sumMap.put(target-nums[i],i);
}
}
}
}