leetcode 349:两个数组的交集I
Problem:
Given two arrays, write a function to compute their intersection.
中文:已知两个数组,写一个函数来计算它们的交集
Example:
Given
nums1 = [1, 2, 2, 1], nums2 = [2, 2],return[2, 2].已知
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 num2’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?
- 假如已知的数组是有序的该如何优化算法?
- 假如数组1的大小比数组2要小该如何优化?
- 假如数组2的元素在磁盘上是排序好的,但是内存不能一次性容纳这些元素,该怎么做?
Solution:
Analysis:
- 1.直接的想法:直接嵌套遍历两个数组,遇到相同元素的就加入一个预定义好的数组中。
- 预定义的数组长度是数组1和数组2中较小的一个。
- 最后将该数组有效的元素重新移植到新的一个数组中。
- 2.修正1:得到的数组可能有很多重复的元素。
- 要再添加新元素到数组中时检查数组中是否已经存在该元素。
- 数组会越界,所以要在添加前检查元素个数是否超过了数组长度。
Code in JAVA
public static int[] intersection(int[] a1, int[] a2) {
int n = Math.min(a1.length, a2.length);
int[] is = new int[n];
int count = 0;
for(int i = 0; i < a1.length; i++){
int tmep = a1[i];
for(int j = 0; j < a2.length; j++){
if(tmep == a2[j]){
boolean exist = false;
for(int k = 0; k < count; k++){
if(is[k] == tmep){
exist = true;
break;
}
}
if(count >= n){
break;
}
if(!exist){
is[count] = tmep;
count++;
}
break;
}
}
}
int[] itersection = new int[count];
for(int i = 0; i < count; i++){
itersection[i] = is[i];
}
return itersection;
}
leetcode 349:两个数组的交集I的更多相关文章
- Java实现 LeetCode 349 两个数组的交集
349. 两个数组的交集 给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入: nums1 = [1,2,2,1], nums2 = [2,2] 输出: [2] 示例 2: 输入: num ...
- Leetcode 349. 两个数组的交集 By Python
给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入: nums1 = [1,2,2,1], nums2 = [2,2] 输出: [2] 示例 2: 输入: nums1 = [4,9,5], ...
- 前端与算法 leetcode 350. 两个数组的交集 II
目录 # 前端与算法 leetcode 350. 两个数组的交集 II 题目描述 概要 提示 解析 解法一:哈希表 解法二:双指针 解法三:暴力法 算法 # 前端与算法 leetcode 350. 两 ...
- Java实现 LeetCode 350 两个数组的交集 II(二)
350. 两个数组的交集 II 给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入: nums1 = [1,2,2,1], nums2 = [2,2] 输出: [2,2] 示例 2: 输入 ...
- leetcode NO.349 两个数组的交集 (python实现)
来源 https://leetcode-cn.com/problems/intersection-of-two-arrays/ 题目描述 给定两个数组,写一个函数来计算它们的交集. 例子: 给定 nu ...
- Leetcode 350.两个数组的交集|| By Python
给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入: nums1 = [1,2,2,1], nums2 = [2,2] 输出: [2,2] 示例 2: 输入: nums1 = [4,9,5 ...
- python(leetcode)-350两个数组的交集
给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入: nums1 = [1,2,2,1], nums2 = [2,2] 输出: [2,2] 示例 2: 输入: nums1 = [4,9,5 ...
- 领扣(LeetCode)两个数组的交集II 个人题解
给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入: nums1 = [1,2,2,1], nums2 = [2,2] 输出: [2,2] 示例 2: 输入: nums1 = [4,9,5 ...
- LeetCode 350: 两个数组的交集 II Intersection of Two Arrays II
题目: 给定两个数组,编写一个函数来计算它们的交集. Given two arrays, write a function to compute their intersection. 示例 1: 输 ...
- [LeetCode] 350. 两个数组的交集 II intersection-of-two-arrays-ii(排序)
思路: 先找到set的交集,然后分别计算交集中的每个元素在两个原始数组中出现的最小次数. class Solution(object): def intersect(self, nums1, nums ...
随机推荐
- c#.net 调用BouncyCastle生成PEM格式的私钥和公钥
RsaKeyPairGenerator r = new RsaKeyPairGenerator(); r.Init()); AsymmetricCipherKeyPair keys = r.Gener ...
- Screen-后台运行
文章转自:http://www.cnblogs.com/mchina/archive/2013/01/30/2880680.html 本来想搜集资料写一个的,结果发现该博主已经总结的很好,条理清晰. ...
- activiti自定义流程之Spring整合activiti-modeler5.16实例(五):流程定义列表
注:(1)环境搭建:activiti自定义流程之Spring整合activiti-modeler5.16实例(一):环境搭建 (2)创建流程模型:activiti自定义流程之Spring ...
- Lnmp环境的自搭建
### 备选#### 安装开发者工具包 (简约版的可能要安装一下) yum groupinstall "Development tools" ########## 1.准备 php ...
- 回到顶部缓动效果代码 --- tween动画函数库
function animateGoTop() { var top = $(document).scrollTop(); var end = 0; var dur = 500; var t = 0; ...
- 尽量采用as操作符而不是旧式C风格做强制类型转换
http://www.cnblogs.com/JiangSoney/archive/2009/08/07/1541488.html MSDN: https://msdn.microsoft.com/z ...
- 黄聪:simple_html_dom 换行符丢失
我在利用simple_html_dom来解析文档是,想要将其中的换行符替换成<BR> , 结果试了好几次没有成功,但是在原始文档中确实是有换行符的.后来索性把装载进来的文档打印出来,结果发 ...
- MySQL key/value存储方案(转)
需求 250M entities, entities表共有2.5亿条记录,当然是分库的. 典型解决方案:RDBMS 问题:由于业务需要不定期更改表结构,但是在2.5亿记录的表上增删字段.修改索引需要锁 ...
- 理解Socket编程【转载】
“一切皆Socket!” 话虽些许夸张,但是事实也是,现在的网络编程几乎都是用的socket. ——有感于实际编程和开源项目研究. 我们深谙信息交流的价值,那网络中进程之间如何通信,如我们每天打开浏览 ...
- Java compiler level does not match the version of the installed Java project facet. springmvc1 和 Target runtime Apache Tomcat v7.0 is not defined.
Java compiler level does not match the version of the installed Java project facet.springmvc1 : Targ ...