LeetCode_350. Intersection of Two Arrays II
350. Intersection of Two Arrays II
Given two arrays, write a function to compute their intersection.
Example 1:
Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2,2]
Example 2:
Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [4,9]
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?
package leetcode.easy; public class IntersectionOfTwoArraysII {
private static void print_arr(int[] nums) {
for (int num : nums) {
System.out.print(num + " ");
}
System.out.println();
} public int[] intersect(int[] nums1, int[] nums2) {
int i = 0, j = 0, k = 0;
int len = nums1.length < nums2.length ? nums1.length : nums2.length;
int[] nums = new int[len];
java.util.Arrays.sort(nums1);
java.util.Arrays.sort(nums2);
while (i < nums1.length && j < nums2.length) {
if (nums1[i] < nums2[j]) {
i++;
} else if (nums1[i] > nums2[j]) {
j++;
} else {
nums[k++] = nums1[i];
i++;
j++;
}
}
return java.util.Arrays.copyOf(nums, k);
} @org.junit.Test
public void test1() {
int[] nums1 = { 1, 2, 2, 1 };
int[] nums2 = { 2, 2 };
print_arr(intersect(nums1, nums2));
} @org.junit.Test
public void test2() {
int[] nums1 = { 4, 9, 5 };
int[] nums2 = { 9, 4, 9, 8, 4 };
print_arr(intersect(nums1, nums2));
}
}
LeetCode_350. Intersection of Two Arrays II的更多相关文章
- 【leetcode】350. Intersection of Two Arrays II
problem 350. Intersection of Two Arrays II 不是特别明白这道题的意思,例子不够说明问题: 是按顺序把相同的元素保存下来,还是排序,但是第二个例子没有重复... ...
- [LintCode] Intersection of Two Arrays II 两个数组相交之二
Given two arrays, write a function to compute their intersection.Notice Each element in the result s ...
- 26. leetcode 350. Intersection of Two Arrays II
350. Intersection of Two Arrays II Given two arrays, write a function to compute their intersection. ...
- [LeetCode] 349 Intersection of Two Arrays && 350 Intersection of Two Arrays II
这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/in ...
- LeetCode Javascript实现 169. Majority Element 217. Contains Duplicate(两个对象比较是否相等时,如果都指向同一个对象,a==b才是true)350. Intersection of Two Arrays II
169. Majority Element /** * @param {number[]} nums * @return {number} */ var majorityElement = funct ...
- [LeetCode] Intersection of Two Arrays II 两个数组相交之二
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- LeetCode Intersection of Two Arrays II
原题链接在这里:https://leetcode.com/problems/intersection-of-two-arrays-ii/ 题目: Given two arrays, write a f ...
- 【一天一道LeetCode】#350. Intersection of Two Arrays II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...
- [LeetCode] 350. Intersection of Two Arrays II 两个数组相交之二
Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...
随机推荐
- 四大网络之Alexnet
本文主要介绍AlextNet的一些知识,这些知识经常被忽略 一.AlextNet的创新点 (1)成功使用ReLU作为CNN的激活函数,并验证其效果在较深的网络超过了Sigmoid,成功解决了Si ...
- RMQ--树状数组,ST表,线段树
RMQ Range Minimum/Maximum Query 区间最值问题 树状数组 https://www.cnblogs.com/xenny/p/9739600.html lowbit(x) x ...
- DVWA暴力破解练习
本周学习内容: 1.结合DVWA学习Web应用安全权威指南 实验内容: 使用BurpSuite工具进行DVWA暴力破解 实验步骤: 1.打开DVWA,进入DVWA Security模块将 Level修 ...
- Bzoj 2440: [中山市选2011]完全平方数(莫比乌斯函数+容斥原理+二分答案)
2440: [中山市选2011]完全平方数 Time Limit: 10 Sec Memory Limit: 128 MB Description 小 X 自幼就很喜欢数.但奇怪的是,他十分讨厌完全平 ...
- NetworkX系列教程(11)-graph和其他数据格式转换
小书匠 Graph 图论 学过线性代数的都了解矩阵,在矩阵上的文章可做的很多,什么特征矩阵,单位矩阵等.grpah存储可以使用矩阵,比如graph的邻接矩阵,权重矩阵等,这节主要是在等到graph后 ...
- PHP隐藏入口脚本文件index.php
一.nginx下 隐藏入口文件时,配置nginx 首先得开启nginx pathinfo模式: location ~ \.php { #去掉$ root E:/phpStudy/WWW/tp/publ ...
- Go程序员面试算法宝典-读后感2-链表
链表作为最基本的数据结构,它不仅仅在实际应用中有着非常重要的作用,而且也是程序员面试笔试必考的内容. 详情请Google吧. 1.如何实现链表的逆序 就地逆序 package main import ...
- Javascript正则RegExp对象replace方法替换url参数值
看别的博客有用eval执行正则表达式的写法, //替换指定传入参数的值,paramName为参数,replaceWith为新值 function replaceParamVal(paramName,r ...
- VirtualAlloc加载shellcode免杀一点记录
一个很好的学习网站 推荐一下: https://docs.microsoft.com/zh-cn/windows/win32/api/ 0x01 VirtualAlloc VirtualAlloc: ...
- 第10组 Alpha冲刺(1/6)
链接部分 队名:女生都队 组长博客: 博客链接 作业博客:博客链接 小组内容 史恩泽(组长) 过去两天完成了哪些任务 描述 了解了反馈机制的实现原理 确定好算法的框架 对接口的规范化进行学习 展示Gi ...