-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoSum.java
More file actions
38 lines (34 loc) · 1.08 KB
/
Copy pathTwoSum.java
File metadata and controls
38 lines (34 loc) · 1.08 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
package com.hemanth;
import java.util.Arrays;
import java.util.HashMap;
/**
* Given a list of numbers, return whether any two sums to k.
* For example, given [10, 15, 3, 7] and k of 17, return true since 10 + 7 is 17.
* Bonus: Can you do this in one pass?
* <p>
* Leetcode link : <a href="https://leetcode.com/problems/two-sum/">Two Sum</a>
* <br>
* Other Variation
* <br>
* Leetcode link : <a href="https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/">Two Sum II - Input Array Is Sorted</a>
* <br>
*
*/
public class TwoSum {
public static void main(String[] args) {
int[] arr = new int[]{10, 15, 3, 7};
int target = 17;
System.out.println(Arrays.toString(twoSum(arr, target)));
}
public static int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> map = new HashMap<>();
for (int num : nums) {
if (map.containsKey(num)) {
return new int[]{num, map.get(num)};
} else {
map.put(target - num, num);
}
}
return new int[]{-1, -1};
}
}