Skip to content

Commit a424440

Browse files
author
谢育欣
committed
两数之和
1 parent 3fbf1da commit a424440

File tree

4 files changed

+13
-8
lines changed

4 files changed

+13
-8
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# Project exclude paths
2-
/out/
2+
/out/
3+
/.idea/

.idea/compiler.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/com/leetcode/Main1.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,19 @@ public int[] twoSum(int[] nums, int target) {
3131
}
3232
return res;
3333
}
34-
public int[] youhua(int[] nums, int target) {
34+
public int[] youhuaTwoSum(int[] nums, int target) {
3535
HashMap<Integer, Integer> map = new HashMap<>();
3636
for (int i = 0; i < nums.length; i++) {
37-
int complement = target - nums[i];
38-
if (map.containsKey(complement)) {
39-
return new int[] { map.get(complement), i };
37+
if (map.containsKey(target - nums[i])) {
38+
return new int[] {map.get(target - nums[i]), i};
4039
}
4140
map.put(nums[i], i);
4241
}
43-
throw new IllegalArgumentException("NO two sum!");
42+
return new int[0];
4443
}
4544
public static void main(String[] args) {
4645
Main1 m = new Main1();
4746
int[] nums = {2, 7, 11, 15};
48-
System.out.println(Arrays.toString(m.twoSum(nums, 9)));
47+
System.out.println(Arrays.toString(m.youhuaTwoSum(nums, 18)));
4948
}
5049
}

0 commit comments

Comments
 (0)