-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path00001-two_sum.java
More file actions
30 lines (23 loc) · 779 Bytes
/
Copy path00001-two_sum.java
File metadata and controls
30 lines (23 loc) · 779 Bytes
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
// 1: Two Sum
// https://leetcode.com/problems/two-sum/
import java.util.HashMap;
import java.util.Map;
class Solution {
// SOLUTION
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i=0; i<nums.length; map.put(nums[i], i++))
if (map.containsKey(target - nums[i]))
return new int[] {map.get(target - nums[i]), i};
return new int[] {0,0};
}
public static void main(String[] args) {
Solution o = new Solution();
// INPUT
int[] nums = {3,2,4};
int target = 6;
// OUTPUT
var result = o.twoSum(nums, target);
System.out.println("[" + result[0] + ", " + result[1] + "]");
}
}