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

Commit 0f458de

Browse files
新增java解法(hashArray)
新增java解法(hashArray)
1 parent 8dc66e3 commit 0f458de

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

‎problems/0349.两个数组的交集.md‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public:
121121
## 其他语言版本
122122

123123
### Java:
124-
124+
版本一:使用HashSet
125125
```Java
126126
import java.util.HashSet;
127127
import java.util.Set;
@@ -159,7 +159,28 @@ class Solution {
159159
}
160160
}
161161
```
162-
162+
版本二:使用Hash數組
163+
```java
164+
class Solution {
165+
public int[] intersection(int[] nums1, int[] nums2) {
166+
int[] hash1 = new int[1002];
167+
int[] hash2 = new int[1002];
168+
for(int i : nums1)
169+
hash1[i]++;
170+
for(int i : nums2)
171+
hash2[i]++;
172+
List<Integer> resList = new ArrayList<>();
173+
for(int i = 0; i < 1002; i++)
174+
if(hash1[i] > 0 && hash2[i] > 0)
175+
resList.add(i);
176+
int index = 0;
177+
int res[] = new int[resList.size()];
178+
for(int i : resList)
179+
res[index++] = i;
180+
return res;
181+
}
182+
}
183+
```
163184
### Python3:
164185
(版本一) 使用字典和集合
165186

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /