Skip to content

Commit 842f0c6

Browse files
two sum
1 parent 8466382 commit 842f0c6

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

EASY/src/easy/TwoSum.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package easy;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class TwoSum {
7+
public int[] twoSum_ON2(int[] nums, int target) {
8+
int[] result = new int[2];
9+
for(int i = 0; i < nums.length-1; i++){
10+
for(int j = i+1; j < nums.length; j++){
11+
if(nums[i] + nums[j] == target){
12+
result[0] = i;
13+
result[1] = j;
14+
break;
15+
}
16+
}
17+
}
18+
return result;
19+
}
20+
21+
public int[] twoSum_ON(int[] nums, int target) {
22+
Map<Integer, Integer> map = new HashMap();
23+
int[] result = new int[2];
24+
for(int i = 0; i < nums.length; i++){
25+
if(map.containsKey(target-nums[i])){
26+
result[0] = map.get(target-nums[i]);
27+
result[1] = i;
28+
break;
29+
} else {
30+
map.put(nums[i], i);
31+
}
32+
}
33+
return result;
34+
}
35+
}

0 commit comments

Comments
 (0)