350. Intersection of Two Arrays II | 查找两个数组重复
来源:互联网 发布:泰国留学知乎 编辑:程序博客网 时间:2024/05/21 06:18
Given two arrays, write a function to compute their intersection.
Example:
Given nums1 = [1, 2, 2, 1]
, nums2 = [2, 2]
, return [2, 2]
.
Note:
- Each element in the result should appear as many times as it shows in both arrays.
- The result can be in any order.
Follow up:
- What if the given array is already sorted? How would you optimize your algorithm?
- What if nums1's size is small compared to nums2's size? Which algorithm is better?
- What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?
Subscribe to see which companies asked this question.
public class Solution { public int[] intersect(int[] nums1, int[] nums2) {int[] intersect;ArrayList<Integer> list = new ArrayList<>();Arrays.sort(nums1);Arrays.sort(nums2);int i, j;i = j = 0;while (i < nums1.length && j < nums2.length) {if (nums1[i] == nums2[j]) {list.add(nums1[i]);i++;j++;} else if (nums1[i] > nums2[j]) {j++;} else {i++;}}intersect = new int[list.size()];for (int k = 0; k < list.size(); k++) {intersect[k] = list.get(k);}return intersect;}}
0 0
- 350. Intersection of Two Arrays II | 查找两个数组重复
- 350. Intersection of Two Arrays II (求两个数组的交集,包含重复元素)
- Leetcode Intersection of Two Arrays i,ii查找两个数组的公共元素
- Intersection of Two Arrays II两个数组交集(重要!)
- Intersection of Two Arrays II 两个数组相交之二
- [LeetCode]350. Intersection of Two Arrays II(求两个数组交集 II)
- leetcode 350. Intersection of Two Arrays II 两个数组的交集
- Leetcode:350. Intersection of Two Arrays II 求两个数组的交集
- LeetCode 350. Intersection of Two Arrays II(数组交集)
- [LeetCode] Intersection of Two Arrays II 两个数组的交集II
- 350. Intersection of Two Arrays II
- LeetCode 350. Intersection of Two Arrays II
- leetcode.350. Intersection of Two Arrays II
- 350. Intersection of Two Arrays II【E】
- 350. Intersection of Two Arrays II
- [leetcode] 350. Intersection of Two Arrays II
- Leetcode 350. Intersection of Two Arrays II
- leetcode 350. Intersection of Two Arrays II
- java 拦截器不拦截静态资源的配置方式
- 图像语义分析--深度学习方法
- hihoCoder #1068 : RMQ-ST算法
- OpenCV中的HOG+SVM物体分类
- linux IO刷新机制sync, fsync, fdatasync和fflush详解及注意事项
- 350. Intersection of Two Arrays II | 查找两个数组重复
- jquery的ajax无法上传空数组
- iOS TextField收取键盘的方法
- UMEditor项目中的引用实例
- 每日一道算法题——Remove Nth Node From End of List
- 全栈之路--JSON
- 数据库的拆分
- Algorithm理解用例:动态规划
- AT&T汇编