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 ...
随机推荐
- Python(正则 Time datatime os sys random json pickle模块)
正则表达式: import re #导入模块名 p = re.compile(-]代表匹配0至9的任意一个数字, 所以这里的意思是对传进来的字符串进行匹配,如果这个字符串的开头第一个字符是数字,就代表 ...
- LintCode "Submatrix Sum"
Naive solution is O(n^4). But on 1 certain dimension, naive O(n^2) can be O(n) by this well-known eq ...
- 【并发编程】Executor类的继承结构
来自为知笔记(Wiz)
- python asyncio笔记
1.什么是coroutine coroutine,最早我是在lua里面看到的,coroutine最大的好处是可以保存堆栈,让程序得以继续执行,在python里面,一般是利用yield来实现,具体可以看 ...
- 103. Binary Tree Zigzag Level Order Traversal
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
- 75. Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- 5. redis管道, 发布订阅, 模拟队列
一. 发布订阅 #订阅scribe 127.0.0.1:6379> SUBSCRIBE "channel_1" Reading messages... (press Ctrl ...
- Javascript之UI线程与性能优化
在浏览器中,Javascript执行与UI更新是发生在同一个进程(浏览器UI线程)中的.UI线程的工作基于一个简单的队列系统,任务会被保存到队列中直到进程空闲时被提取出来执行.所以Javascript ...
- C#学习笔记五: C#3.0Lambda表达式及Linq解析
最早使用到Lambda表达式是因为一个需求:如果一个数组是:int[] s = new int[]{1,3,5,9,14,16,22};例如只想要这个数组中小于15的元素然后重新组装成一个数组或者直接 ...
- CentOS of MySQL command
1.本地连接数据库 [root@iZ253lxv4i0Z mysql]# mysql -u root -pEnter password: or: [root@iZ253lxv4i0Z mysql]# ...