mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
添加349. 两个数组的交集JavaScript版本
This commit is contained in:
@ -113,6 +113,34 @@ Python:
|
|||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
|
javaScript:
|
||||||
|
|
||||||
|
```js
|
||||||
|
/**
|
||||||
|
* @param {number[]} nums1
|
||||||
|
* @param {number[]} nums2
|
||||||
|
* @return {number[]}
|
||||||
|
*/
|
||||||
|
var intersection = function(nums1, nums2) {
|
||||||
|
// 根据数组大小交换操作的数组
|
||||||
|
if(nums1.length < nums2.length) {
|
||||||
|
const _ = nums1;
|
||||||
|
nums1 = nums2;
|
||||||
|
nums2 = _;
|
||||||
|
}
|
||||||
|
const nums1Set = new Set(nums1);
|
||||||
|
const resSet = new Set();
|
||||||
|
// for(const n of nums2) {
|
||||||
|
// nums1Set.has(n) && resSet.add(n);
|
||||||
|
// }
|
||||||
|
// 循环 比 迭代器快
|
||||||
|
for(let i = nums2.length - 1; i >= 0; i--) {
|
||||||
|
nums1Set.has(nums2[i]) && resSet.add(nums2[i]);
|
||||||
|
}
|
||||||
|
return Array.from(resSet);
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user