350. Intersection of Two Arrays II
2024-09-11 15:02:09
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.
代码如下:
public class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
List<Integer> list=new ArrayList<>(); Arrays.sort(nums1);
Arrays.sort(nums2); for(int i=0,j=0;i<nums1.length&&j<nums2.length;)
{
if(nums1[i]==nums2[j])
{
list.add(nums1[i]);
i++;
j++;
}
else if(nums1[i]<nums2[j])
i++;
else if(nums1[i]>nums2[j])
j++;
}
int[] result=new int[list.size()];
for(int i=0;i<list.size();i++)
result[i]=list.get(i); return result; }
}
最新文章
- Werewolf流程分析
- 优雅的使用python之环境管理
- mysql pid文件
- u1-nav-css
- SQL实现字段内容查找和替换
- Log4Net学习【三】
- 利用接口做参数,写个计算器,能完成+-*/运算 (1)定义一个接口Compute含有一个方法int computer(int n,int m); (2)设计四个类分别实现此接口,完成+-*/运算 (3)设计一个类UseCompute,含有方法: public void useCom(Compute com, int one, int two) 此方法要求能够:1.用传递过来的对象调用compute
- 嵌入式 linux 查看内存
- mysql binaryVInstall
- mongodb数据库调试问题:‘db object already connecting, open cannot be called multiple times’
- Ubuntu14.04安装一个小问题,搜狗输入法
- 计算机网络课程优秀备考PPT之第七章应用层(七)
- require include 一个隐藏的用法:作用域。
- TCP协议随笔
- .Net Core文件上传
- 3.ansible-iventory的写法和基本变量
- Python 语法提示vim配置
- pop
- 关于gzip zgrep zcat 的使用
- Nginx range filter模块数字错误漏洞修复 (Nginx平滑升级)