File tree 13 files changed +270
-72
lines changed
0349.Intersection of Two Arrays
0350.Intersection of Two Arrays II
13 files changed +270
-72
lines changed Original file line number Diff line number Diff line change 35
35
36
36
<!-- 这里可写通用的实现逻辑 -->
37
37
38
+ “哈希表”实现。
39
+
38
40
<!-- tabs:start -->
39
41
40
42
### ** Python3**
44
46
``` python
45
47
class Solution :
46
48
def intersection (self , nums1 : List[int ], nums2 : List[int ]) -> List[int ]:
47
- s1, s2 = set (nums1), set (nums2)
48
- return list (s1 & s2)
49
+ s = set (nums1)
50
+ res = set ()
51
+ for num in nums2:
52
+ if num in s:
53
+ res.add(num)
54
+ return list (res)
49
55
```
50
56
51
57
### ** Java**
@@ -55,25 +61,47 @@ class Solution:
55
61
``` java
56
62
class Solution {
57
63
public int [] intersection (int [] nums1 , int [] nums2 ) {
58
- Set<Integer > s1 = transfer(nums1);
59
- Set<Integer > s2 = transfer(nums2);
60
- s1. retainAll(s2);
61
- int [] output = new int [s1. size()];
64
+ Set<Integer > s = new HashSet<> ();
65
+ for (int num : nums1) {
66
+ s. add(num);
67
+ }
68
+ Set<Integer > res = new HashSet<> ();
69
+ for (int num : nums2) {
70
+ if (s. contains(num)) {
71
+ res. add(num);
72
+ }
73
+ }
74
+ int [] output = new int [res. size()];
62
75
int i = 0 ;
63
- for (Integer e : s1 ) {
64
- output[i++ ] = e ;
76
+ for (int num : res ) {
77
+ output[i++ ] = num ;
65
78
}
66
79
return output;
67
80
}
81
+ }
82
+ ```
68
83
69
- private Set<Integer > transfer (int [] nums ) {
70
- Set<Integer > s = new HashSet<> ();
71
- for (int e : nums) {
72
- s. add(e);
84
+ ### ** JavaScript**
85
+
86
+ ``` js
87
+ /**
88
+ * @param {number[]} nums1
89
+ * @param {number[]} nums2
90
+ * @return {number[]}
91
+ */
92
+ var intersection = function (nums1 , nums2 ) {
93
+ const s = new Set ();
94
+ for (const num of nums1) {
95
+ s .add (num);
96
+ }
97
+ let res = new Set ();
98
+ for (const num of nums2) {
99
+ if (s .has (num)) {
100
+ res .add (num);
73
101
}
74
- return s;
75
102
}
76
- }
103
+ return [... res];
104
+ };
77
105
```
78
106
79
107
### ** ...**
Original file line number Diff line number Diff line change 40
40
``` python
41
41
class Solution :
42
42
def intersection (self , nums1 : List[int ], nums2 : List[int ]) -> List[int ]:
43
- s1, s2 = set (nums1), set (nums2)
44
- return list (s1 & s2)
43
+ s = set (nums1)
44
+ res = set ()
45
+ for num in nums2:
46
+ if num in s:
47
+ res.add(num)
48
+ return list (res)
45
49
```
46
50
47
51
### ** Java**
48
52
49
53
``` java
50
54
class Solution {
51
55
public int [] intersection (int [] nums1 , int [] nums2 ) {
52
- Set<Integer > s1 = transfer(nums1);
53
- Set<Integer > s2 = transfer(nums2);
54
- s1. retainAll(s2);
55
- int [] output = new int [s1. size()];
56
+ Set<Integer > s = new HashSet<> ();
57
+ for (int num : nums1) {
58
+ s. add(num);
59
+ }
60
+ Set<Integer > res = new HashSet<> ();
61
+ for (int num : nums2) {
62
+ if (s. contains(num)) {
63
+ res. add(num);
64
+ }
65
+ }
66
+ int [] output = new int [res. size()];
56
67
int i = 0 ;
57
- for (Integer e : s1 ) {
58
- output[i++ ] = e ;
68
+ for (int num : res ) {
69
+ output[i++ ] = num ;
59
70
}
60
71
return output;
61
72
}
73
+ }
74
+ ```
62
75
63
- private Set<Integer > transfer (int [] nums ) {
64
- Set<Integer > s = new HashSet<> ();
65
- for (int e : nums) {
66
- s. add(e);
76
+ ### ** JavaScript**
77
+
78
+ ``` js
79
+ /**
80
+ * @param {number[]} nums1
81
+ * @param {number[]} nums2
82
+ * @return {number[]}
83
+ */
84
+ var intersection = function (nums1 , nums2 ) {
85
+ const s = new Set ();
86
+ for (const num of nums1) {
87
+ s .add (num);
88
+ }
89
+ let res = new Set ();
90
+ for (const num of nums2) {
91
+ if (s .has (num)) {
92
+ res .add (num);
67
93
}
68
- return s;
69
94
}
70
- }
95
+ return [... res];
96
+ };
71
97
```
72
98
73
99
### ** ...**
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public int [] intersection (int [] nums1 , int [] nums2 ) {
3
- Set <Integer > s1 = transfer (nums1 );
4
- Set <Integer > s2 = transfer (nums2 );
5
- s1 .retainAll (s2 );
6
- int [] output = new int [s1 .size ()];
3
+ Set <Integer > s = new HashSet <>();
4
+ for (int num : nums1 ) {
5
+ s .add (num );
6
+ }
7
+ Set <Integer > res = new HashSet <>();
8
+ for (int num : nums2 ) {
9
+ if (s .contains (num )) {
10
+ res .add (num );
11
+ }
12
+ }
13
+ int [] output = new int [res .size ()];
7
14
int i = 0 ;
8
- for (Integer e : s1 ) {
9
- output [i ++] = e ;
15
+ for (int num : res ) {
16
+ output [i ++] = num ;
10
17
}
11
18
return output ;
12
19
}
13
-
14
- private Set <Integer > transfer (int [] nums ) {
15
- Set <Integer > s = new HashSet <>();
16
- for (int e : nums ) {
17
- s .add (e );
18
- }
19
- return s ;
20
- }
21
20
}
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[] } nums1
3
+ * @param {number[] } nums2
4
+ * @return {number[] }
5
+ */
6
+ var intersection = function ( nums1 , nums2 ) {
7
+ const s = new Set ( ) ;
8
+ for ( const num of nums1 ) {
9
+ s . add ( num ) ;
10
+ }
11
+ let res = new Set ( ) ;
12
+ for ( const num of nums2 ) {
13
+ if ( s . has ( num ) ) {
14
+ res . add ( num ) ;
15
+ }
16
+ }
17
+ return [ ...res ] ;
18
+ } ;
Original file line number Diff line number Diff line change 1
1
class Solution :
2
2
def intersection (self , nums1 : List [int ], nums2 : List [int ]) -> List [int ]:
3
- s1 , s2 = set (nums1 ), set (nums2 )
4
- return list (s1 & s2 )
3
+ s = set (nums1 )
4
+ res = set ()
5
+ for num in nums2 :
6
+ if num in s :
7
+ res .add (num )
8
+ return list (res )
Original file line number Diff line number Diff line change 43
43
44
44
<!-- 这里可写通用的实现逻辑 -->
45
45
46
+ “哈希表”实现。
47
+
46
48
<!-- tabs:start -->
47
49
48
50
### ** Python3**
49
51
50
52
<!-- 这里可写当前语言的特殊实现逻辑 -->
51
53
52
54
``` python
53
-
55
+ class Solution :
56
+ def intersect (self , nums1 : List[int ], nums2 : List[int ]) -> List[int ]:
57
+ counter = collections.Counter(nums1)
58
+ res = []
59
+ for num in nums2:
60
+ if counter[num] > 0 :
61
+ res.append(num)
62
+ counter[num] -= 1
63
+ return res
54
64
```
55
65
56
66
### ** Java**
57
67
58
68
<!-- 这里可写当前语言的特殊实现逻辑 -->
59
69
60
70
``` java
71
+ class Solution {
72
+ public int [] intersect (int [] nums1 , int [] nums2 ) {
73
+ Map<Integer , Integer > counter = new HashMap<> ();
74
+ for (int num : nums1) {
75
+ counter. put(num, counter. getOrDefault(num, 0 ) + 1 );
76
+ }
77
+ List<Integer > intersection = new ArrayList<> ();
78
+ for (int num : nums2) {
79
+ int val = counter. getOrDefault(num, 0 );
80
+ if (val > 0 ) {
81
+ intersection. add(num);
82
+ counter. put(num, val - 1 );
83
+ }
84
+ }
85
+ int i = 0 ;
86
+ int [] res = new int [intersection. size()];
87
+ for (int num : intersection) {
88
+ res[i++ ] = num;
89
+ }
90
+ return res;
91
+ }
92
+ }
93
+ ```
61
94
95
+ ### ** JavaScript**
96
+
97
+ ``` js
98
+ /**
99
+ * @param {number[]} nums1
100
+ * @param {number[]} nums2
101
+ * @return {number[]}
102
+ */
103
+ var intersect = function (nums1 , nums2 ) {
104
+ const counter = {};
105
+ for (const num of nums1) {
106
+ counter[num] = (counter[num] || 0 ) + 1 ;
107
+ }
108
+ let res = [];
109
+ for (const num of nums2) {
110
+ if (counter[num] > 0 ) {
111
+ res .push (num);
112
+ counter[num] -= 1 ;
113
+ }
114
+ }
115
+ return res;
116
+ };
62
117
```
63
118
64
119
### ** ...**
Original file line number Diff line number Diff line change 47
47
### ** Python3**
48
48
49
49
``` python
50
-
50
+ class Solution :
51
+ def intersect (self , nums1 : List[int ], nums2 : List[int ]) -> List[int ]:
52
+ counter = collections.Counter(nums1)
53
+ res = []
54
+ for num in nums2:
55
+ if counter[num] > 0 :
56
+ res.append(num)
57
+ counter[num] -= 1
58
+ return res
51
59
```
52
60
53
61
### ** Java**
54
62
55
63
``` java
56
-
64
+ class Solution {
65
+ public int [] intersect (int [] nums1 , int [] nums2 ) {
66
+ Map<Integer , Integer > counter = new HashMap<> ();
67
+ for (int num : nums1) {
68
+ counter. put(num, counter. getOrDefault(num, 0 ) + 1 );
69
+ }
70
+ List<Integer > intersection = new ArrayList<> ();
71
+ for (int num : nums2) {
72
+ int val = counter. getOrDefault(num, 0 );
73
+ if (val > 0 ) {
74
+ intersection. add(num);
75
+ counter. put(num, val - 1 );
76
+ }
77
+ }
78
+ int i = 0 ;
79
+ int [] res = new int [intersection. size()];
80
+ for (int num : intersection) {
81
+ res[i++ ] = num;
82
+ }
83
+ return res;
84
+ }
85
+ }
57
86
```
58
87
88
+ ### ** JavaScript**
89
+
90
+ ``` js
91
+ /**
92
+ * @param {number[]} nums1
93
+ * @param {number[]} nums2
94
+ * @return {number[]}
95
+ */
96
+ var intersect = function (nums1 , nums2 ) {
97
+ const counter = {};
98
+ for (const num of nums1) {
99
+ counter[num] = (counter[num] || 0 ) + 1 ;
100
+ }
101
+ let res = [];
102
+ for (const num of nums2) {
103
+ if (counter[num] > 0 ) {
104
+ res .push (num);
105
+ counter[num] -= 1 ;
106
+ }
107
+ }
108
+ return res;
109
+ };
110
+
59
111
### ** ... **
60
112
61
113
```
You can’t perform that action at this time.
0 commit comments