问题分析:

用两个指针分别遍历即可。

问题求解:

public class Solution {
/**
* @param nums1 an integer array
* @param nums2 an integer array
* @return an integer array
*/
public int[] intersection(int[] nums1, int[] nums2) {
List<Integer> list = new ArrayList<Integer>();
Arrays.sort(nums1);
Arrays.sort(nums2);
int i = 0, j = 0;
while (i < nums1.length && j < nums2.length) {
if(nums1[i] < nums2[j]){
i++;
}else if(nums1[i] == nums2[j]){
list.add(nums1[i]);
i++;
j++;
}else{
j++;
}
}
int[] inter = new int[list.size()];
for (i = 0; i < inter.length; i++) {
inter[i] = list.get(i);
}
return inter;
}
}

[LintCode]计算两个数的交集(二)的更多相关文章

  1. [LintCode]计算两个数的交集(一)

    问题分析: 既然返回值没有重复,我们不妨将结果放进set中,然后对两个set进行比较. 问题求解: public class Solution { /** * @param nums1 an inte ...

  2. 【LintCode】计算两个数的交集(二)

    问题分析: 用两个指针分别遍历即可. 问题求解: public class Solution { /** * @param nums1 an integer array * @param nums2 ...

  3. 【LintCode】计算两个数的交集(一)

    问题分析: 既然返回值没有重复,我们不妨将结果放进set中,然后对两个set进行比较. 问题求解: public class Solution { /** * @param nums1 an inte ...

  4. LeetCode 29 Divide Two Integers (不使用乘法,除法,求模计算两个数的除法)

    题目链接: https://leetcode.com/problems/divide-two-integers/?tab=Description   Problem :不使用乘法,除法,求模计算两个数 ...

  5. grep和map计算两个集合交集、并集、补集

    #!/usr/bin/perl use strict; ######################################## 用grep 和map 获取两个列表的交集并集.补集###### ...

  6. python计算两个数的百分比

    a和b是整数,计算a/b的百分比 a=3 b=7 a=float(a) b=float(b) 保留百分比后2位小数 print  "%.2f%%" % (a/b*100) '42. ...

  7. 用shell脚本 计算两个数的加减乘除取余

    #! /bin/bash # read -p '请输入数:' a //输入 read -p '请输入数:' b echo '$a+$b=' $(( a + b )) //输出 echo '$a-$b= ...

  8. 编程之法section II: 2.2 和为定值的两个数

    ====数组篇==== 2.2 求和为定值的两个数: 题目描述:有n个整数,找出其中满足两数相加为target的两个数(如果有多组满足,只需要找出其中一组),要求时间复杂度尽可能低. 解法一: 思路: ...

  9. PHP计算两个时间段是否有交集(边界重叠不算)

    优化前的版本: /** * PHP计算两个时间段是否有交集(边界重叠不算) * * @param string $beginTime1 开始时间1 * @param string $endTime1 ...

随机推荐

  1. webdriver API study

    This chapter cover all the interfaces of Selenium WebDriver. Recommended Import Style The API defini ...

  2. du和df命令的区别

    du和df命令都被用于获得文件系统大小的信息:df用于报告文件系统的总块数及剩余块数,du -s /<filesystem>用于报告文件系统使用的块数.但是,我们可以发现从df命令算出的文 ...

  3. jquery 事件注冊 与反复事件处理

    <!doctype html> <html lang="us"> <head> <meta charset="utf-8&quo ...

  4. sqlserver把小数点后面多余的0去掉

    Sql中想把小数点后多余的0去掉,怎么办? select 5000/10000.0 --想变成0.5select 5500/10000.0 --想变成0.55select 5550/10000.0 - ...

  5. NRF24L01

    转http://blog.csdn.net/wangshenzhen123/article/details/47207837 1.一个发送通道,6个接收通道.发送端发送通道地址寄存器中的地址指向的就是 ...

  6. telnet远程登录协议

    什么是Telnet? 对于Telnet的认识,不同的人持有不同的观点,可以把Telnet当成一种通信协议,但是对于入侵者而言,Telnet只是一种远程登录的工具.一旦入侵者与远程主机建立了Telnet ...

  7. android获取对话框文本注意事项

    1.View注意设置成final类型如final View layout=.. . 2.获取文本框对象时候格式EditText e = (EditText)layout.findViewById(R. ...

  8. 删除计算机里设备和驱动器中的爱奇艺、PPS、百度云、360云盘图标

    转自:http://jingyan.baidu.com/article/86f4a73e59bb3037d6526936.html 点击"开始"找到"运行" 输 ...

  9. linux 最常用的yum源remi

    Remi repository是包含最新版本php和mysql包的linux源. . Enterprise Linux (with EPEL) x86_64 wget https://dl.fedor ...

  10. ubuntu 14.04英文环境设置成中文

    适用于ubuntu 14.04英文版的系统,其它版本的设置应该是大同小异的. 进入ubuntu系统,在顶部齿状标志找到system... 2.在personal找到Language Support 3 ...