Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0b5f128

Browse files
authored
Two Sum
1 parentb53d170 commit0b5f128

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

‎Source Code/1. Two Sum/README.md‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,30 @@ func twoSum(nums []int, target int) []int {
2828
return []int{}
2929
}
3030
```
31+
32+
##Java
33+
```
34+
class Solution {
35+
public int[] twoSum(int[] nums, int target) {
36+
37+
int n = nums.length;
38+
int[] arr = new int[2];
39+
HashMap < Integer, Integer > hm = new HashMap < Integer, Integer > ();
40+
41+
for (int i = 0; i < n; i++) {
42+
hm.put(nums[i], i);
43+
}
44+
45+
for (int i = 0; i < n; i++) {
46+
if (hm.containsKey(target - nums[i]) && i != hm.get(target - nums[i])) {
47+
arr[0] = i;
48+
arr[1] = hm.get(target - nums[i]);
49+
break;
50+
}
51+
52+
}
53+
54+
return arr;
55+
}
56+
}
57+
```

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp