We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
给定两个数组,编写一个函数来计算它们的交集。
示例 1:
输入: nums1 = [1,2,2,1], nums2 = [2,2] 输出: [2,2]
示例 2:
输入: nums1 = [4,9,5], nums2 = [9,4,9,8,4] 输出: [4,9]
说明:
进阶:
/** * @param {number[]} nums1 * @param {number[]} nums2 * @return {number[]} */ var intersect = function(nums1, nums2) { let map = new Map(); let result = []; for(let i = 0;i<nums1.length;i++){ let value = map.get(nums1[i]); if(value>0){ map.set(nums1[i],++value); }else{ map.set(nums1[i],1); } } for(let i = 0 ;i<nums2.length;i++){ let value = map.get(nums2[i]); if(value>0){ result.push(nums2[i]); map.set(nums2[i],--value); } } return result; };
The text was updated successfully, but these errors were encountered:
No branches or pull requests
两个数组的交集II
一、题目详情
给定两个数组,编写一个函数来计算它们的交集。
示例 1:
示例 2:
说明:
进阶:
二、解题思路
三、代码实现
参考:
The text was updated successfully, but these errors were encountered: