Skip to content

Commit 0a4aa77

Browse files
committed
two sum solved
1 parent 8bcaad5 commit 0a4aa77

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

two-sum/sora0319.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.util.*;
2+
class Solution {
3+
public int[] twoSum(int[] nums, int target) {
4+
Map<Integer,Integer> element = new HashMap<>();
5+
6+
for(int i = 0; i < nums.length; i++){
7+
element.put(i, nums[i]);
8+
}
9+
10+
int[] result = new int[2];
11+
int n = 0;
12+
for(int i = 0; i < nums.length; i++){
13+
element.remove(i);
14+
n = target - nums[i];
15+
if(element.containsValue(n)){
16+
result[0] = i;
17+
break;
18+
}
19+
}
20+
21+
for(int i = 0; i < nums.length; i++){
22+
if(nums[i] == n && i != result[0]){
23+
result[1] = i;
24+
break;
25+
}
26+
}
27+
return result;
28+
}
29+
}
30+

0 commit comments

Comments
 (0)