Skip to content

Commit 139761a

Browse files
committed
添加349. 两个数组的交集JavaScript版本
1 parent fc04e9e commit 139761a

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,34 @@ Python:
113113

114114
Go:
115115

116+
javaScript:
117+
118+
```js
119+
/**
120+
* @param {number[]} nums1
121+
* @param {number[]} nums2
122+
* @return {number[]}
123+
*/
124+
var intersection = function(nums1, nums2) {
125+
// 根据数组大小交换操作的数组
126+
if(nums1.length < nums2.length) {
127+
const _ = nums1;
128+
nums1 = nums2;
129+
nums2 = _;
130+
}
131+
const nums1Set = new Set(nums1);
132+
const resSet = new Set();
133+
// for(const n of nums2) {
134+
// nums1Set.has(n) && resSet.add(n);
135+
// }
136+
// 循环 比 迭代器快
137+
for(let i = nums2.length - 1; i >= 0; i--) {
138+
nums1Set.has(nums2[i]) && resSet.add(nums2[i]);
139+
}
140+
return Array.from(resSet);
141+
};
142+
```
143+
116144

117145

118146

0 commit comments

Comments
 (0)