leecode刷题(6)-- 两个数组的交集II
leecode刷题(6)-- 两个数组的交集II
两个数组的交集II
描述:
给定两个数组,编写一个函数来计算它们的交集。
示例:
输入: nums1 = [1,2,2,1], nums2 = [2,2]
输出: [2,2]
说明:
- 输出结果中每个元素出现的次数,应与元素在两个数组中出现的次数一致。
- 我们可以不考虑输出结果的顺序。
思路:
我们可以用遍历穷举的方法,但是时间复杂度肯定很高。不妨换个思路:先将数组递增排序,排序之后将两个数组同时遍历(定义两个数组的脚本变量,初始值为0,向后遍历),比较同索引位置的元素是否相等,如果相等,则记录下该值;如果不相等,将值较小的数组的脚标加1,另一个数组的脚标等待。然后继续遍历比较,直到遍历完短的数组。
代码如下:
import java.util.Arrays;
public class Intersect {
public int[] intersect (int[] nums1, int[] nums2) {
if (nums1 == null || nums2 == null || nums1.length == 0 || nums2.length == 0) {
return new int[]{};
}
Arrays.sort(nums1);
Arrays.sort(nums2);
int i = 0, j = 0, k = 0;
int[] result = new int[Math.min(nums1.length, nums2.length)];
while (i < nums1.length && j < nums2.length) {
if (nums1[i] == nums2[j]) {
result[k++] = nums1[i];
i++;
j++;
continue;
} else if (nums1[i] > nums2[j]) {
j++;
} else {
i++;
}
}
return Arrays.copyOf(result, k); //拷贝数组
}
public static void main(String[] args) {
int[] nums1 = {1,2,2,1};
int[] nums2 = {2,2};
Intersect intersect = new Intersect();
int[] result = intersect.intersect(nums1, nums2);
//Arrays.toString()方法将hash值转为数组
System.out.println(Arrays.toString(result));
}
}
leecode刷题(6)-- 两个数组的交集II的更多相关文章
- C#LeetCode刷题之#350-两个数组的交集 II(Intersection of Two Arrays II)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4044 访问. 给定两个数组,编写一个函数来计算它们的交集. 输入 ...
- 前端与算法 leetcode 350. 两个数组的交集 II
目录 # 前端与算法 leetcode 350. 两个数组的交集 II 题目描述 概要 提示 解析 解法一:哈希表 解法二:双指针 解法三:暴力法 算法 # 前端与算法 leetcode 350. 两 ...
- 6、两个数组的交集 II
6.两个数组的交集 II 给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入: nums1 = [1,2,2,1], nums2 = [2,2] 输出: [2,2] 示例 2: 输入: n ...
- 【Leetcode】【简单】【350. 两个数组的交集 II】【JavaScript】
题目描述 350. 两个数组的交集 II 给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入: nums1 = [1,2,2,1], nums2 = [2,2]输出: [2,2] 示例 2 ...
- Java实现 LeetCode 350 两个数组的交集 II(二)
350. 两个数组的交集 II 给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入: nums1 = [1,2,2,1], nums2 = [2,2] 输出: [2,2] 示例 2: 输入 ...
- LeetCode初级算法之数组:350 两个数组的交集 II
两个数组的交集 II 题目地址:https://leetcode-cn.com/problems/intersection-of-two-arrays-ii/ 给定两个数组,编写一个函数来计算它们的交 ...
- 【leetcode 简单】 第八十五题 两个数组的交集 II
给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入: nums1 = [1,2,2,1], nums2 = [2,2] 输出: [2,2] 示例 2: 输入: nums1 = [4,9,5 ...
- C#LeetCode刷题之#349-两个数组的交集(Intersection of Two Arrays)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4042 访问. 给定两个数组,编写一个函数来计算它们的交集. 输入 ...
- 两个数组的交集 II [ LeetCode - 350 ]
原题地址:https://leetcode-cn.com/problems/intersection-of-two-arrays-ii/description/ 给定两个数组,写一个方法来计算 ...
随机推荐
- java - 只输出中文, 包含中文标点符号
在 Java 中直接使用Unicode 转码时会按照UTF-16LE 的方式拆分,并加上 BOM. 如果采用 UTF-16 拆分,在 Java 中默认采用带有 BOM 的 UTF-16BE 拆分. S ...
- Dev TreeList基本用法
public partial class treelist_shijian : DevExpress.XtraEditors.XtraForm { public treel ...
- VS2017自动添加头部注释
让VS自动生成类的头部注释,只需修改两个文集即可,一下两个路径下个有一个 Class.cs文件 D:\Program Files (x86)\Microsoft Visual Studio\2017\ ...
- Python基础学习七 Excel操作
python操作excel,python操作excel使用xlrd.xlwt和xlutils模块, xlrd模块是读取excel的,xlwt模块是写excel的,xlutils是用来修改excel的. ...
- Biorhythms(中国剩余定理(模板题))
Description Some people believe that there are three cycles in a person's life that start the day he ...
- PC 微信页面倒计时代码 safari不兼容date的问题
PC: 1.html页面: <div class="aTime"> <em id="t_d"></em> <em id ...
- HTML5样式、链接和表格
-------------------siwuxie095 HTML5 样式 1.标签 <style> 标签:样式定义 <link> 标签:资源引用 2.属性 rel:用于指定 ...
- python3--列表生成式
# Auther: Aaron Fan # 原始的写法:a = []for i in range(10): a.append(i*2)print(a) # 用列表生成式完成上面的写法:a = [i*2 ...
- 利用AdaBoost方法构建多个弱分类器进行分类
1.AdaBoost 思想 补充:这里的若分类器之间有比较强的依赖关系;对于若依赖关系的分类器一般使用Bagging的方法 弱分类器是指分类效果要比随机猜测效果略好的分类器,我们可以通过构建多个弱分类 ...
- jquery表单数据反序列化为字典
.前台代码 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1 ...