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; }
}

最新文章

  1. Werewolf流程分析
  2. 优雅的使用python之环境管理
  3. mysql pid文件
  4. u1-nav-css
  5. SQL实现字段内容查找和替换
  6. Log4Net学习【三】
  7. 利用接口做参数,写个计算器,能完成+-*/运算 (1)定义一个接口Compute含有一个方法int computer(int n,int m); (2)设计四个类分别实现此接口,完成+-*/运算 (3)设计一个类UseCompute,含有方法: public void useCom(Compute com, int one, int two) 此方法要求能够:1.用传递过来的对象调用compute
  8. 嵌入式 linux 查看内存
  9. mysql binaryVInstall
  10. mongodb数据库调试问题:‘db object already connecting, open cannot be called multiple times’
  11. Ubuntu14.04安装一个小问题,搜狗输入法
  12. 计算机网络课程优秀备考PPT之第七章应用层(七)
  13. require include 一个隐藏的用法:作用域。
  14. TCP协议随笔
  15. .Net Core文件上传
  16. 3.ansible-iventory的写法和基本变量
  17. Python 语法提示vim配置
  18. pop
  19. 关于gzip zgrep zcat 的使用
  20. Nginx range filter模块数字错误漏洞修复 (Nginx平滑升级)

热门文章

  1. 两天以来对plsqldev操作的记忆
  2. ZOJ 3654 Letty&#39;s Math Class 模拟 难度:0
  3. IT公司100题-8-智力题
  4. Android设计画面中有EditText时取消启动时自动获得焦点调用系统输入法的方法
  5. bzoj 2154 莫比乌斯反演求lcm的和
  6. JNI与NDK简介
  7. ios开发逆向传值的几种方法整理
  8. RPI学习--环境搭建_无线网络的连接
  9. matlab blkproc
  10. iphone判断当前网络连接类型