We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent da0674b commit 2d987c5Copy full SHA for 2d987c5
04-Using-Hash-Table/Course Code (Java)/04-Two-Sum/src/Solution2.java
@@ -21,13 +21,11 @@ public int[] twoSum(int[] nums, int target) {
21
22
for(int i = 0 ; i < nums.length; i ++){
23
24
- if(record.containsKey(target - nums[i]))
25
- if(record.get(target - nums[i]) != i){
26
- int[] res = {i, record.get(target - nums[i])};
27
- return res;
28
- }
29
-
30
- record.put(nums[i], i);
+ Integer index = record.get(target - nums[i]);
+ if(index != null && index != i){
+ int[] res = {i, index};
+ return res;
+ }
31
}
32
33
throw new IllegalStateException("the input has no solution");
0 commit comments