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

Commit7783f3f

Browse files
all solutions of Intersection of Two Arrays
1 parent172be00 commit7783f3f

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

‎EASY/src/easy/IntersectionOfTwoArrays.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,28 @@ public class IntersectionOfTwoArrays {
2222
//then I clicked its Tags, and find it's marked with so many tags: Binary Search, HashTable, Two Pointers, Sort, now I'll try to do it one by one
2323
//inspired by this post: https://discuss.leetcode.com/topic/45685/three-java-solutions
2424
publicint[]intersection_two_pointers(int[]nums1,int[]nums2) {
25-
25+
Set<Integer>set =newHashSet();
26+
Arrays.sort(nums1);
27+
Arrays.sort(nums2);
28+
inti =0,j =0;
29+
for(;i <nums1.length &&j <nums2.length;){
30+
if(nums1[i] <nums2[j]){
31+
i++;
32+
}elseif(nums1[i] >nums2[j]){
33+
j++;
34+
}else {
35+
set.add(nums1[i]);
36+
i++;
37+
j++;
38+
}
39+
}
40+
int[]result =newint[set.size()];
41+
Iterator<Integer>it =set.iterator();
42+
intk =0;
43+
while(it.hasNext()){
44+
result[k++] =it.next();
45+
}
46+
returnresult;
2647
}
2748

2849
publicint[]intersection_binary_search(int[]nums1,int[]nums2) {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp