Skip to content

Commit b092901

Browse files
committed
Two Sum - Leetcode
Finding two numbers in the array summing up to a target value.
0 parents  commit b092901

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

Sum/.DS_Store

6 KB
Binary file not shown.

Sum/TwoSum.java

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int[] twoSum(int[] nums, int target) {
3+
int[] result = new int[2];
4+
Map<Integer,Integer> map = new HashMap<Integer,Integer>();
5+
for (int i=0; i<nums.length; i++) {
6+
if (map.containsKey(target - nums[i])) {
7+
result[1] = i;
8+
result[0] = map.get(target - nums[i]);
9+
return result;
10+
}
11+
map.put(nums[i],i);
12+
}
13+
return result;
14+
}
15+
}

0 commit comments

Comments
 (0)