问题分析:

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

问题求解:

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. Android开发之模仿UC浏览器的菜单

    这个内容内容涉及到的了两个知识点: PopupWindow:使用PopupWindow创建一个简单的菜单 使用TabHost创建标签:这个网上好多教程随便看看就好. 实现思路: 观察一下UC浏览器的菜 ...

  2. CentOS上搭建Nginx + Mono 运行 asp.net[转]

    http://www.linuxdot.net/ http://www.cnblogs.com/wander1129/archive/2011/12/16/mono.html 安装步骤: 一.获取开源 ...

  3. 使用Xcode、Android Studio将项目链接到Git

    一.使用Android Studio创建本地git仓库: 1.检查本地git环境:在Android Studio中setting-->Version Control 点击Test按钮,提示suc ...

  4. class threading.Thread()说明:

    class threading.Thread()说明: class threading.Thread(group=None, target=None, name=None, args=(), kwar ...

  5. pcurl 学习 http文件下载及写入空设备文件实例

    import pycurl import sys import os import random import string import time class Transaction(object) ...

  6. android自动弹出软键盘(输入键盘)

        很多应用中对于一个界面比如进入搜索界面或者修改信息等等情况,为了用户体验应该自动弹出软键盘而不是让用户主动点击输入框才弹出(因为用户进入该界面必然是为了更改信息).具体实现这种效果如下: [代 ...

  7. lua学习笔记13:协程具体解释和举例

    一.coroutine.create创建协程 參数是协程的主函数,返回一个thread对象 co = coroutine.create(function() print("coroutine ...

  8. Android studio 使用NDK工具实现JNI编程

    前言: Android开发中常常会使用到第三方的.so库.在使用.so库的时候就要用到JNI编程.JNI是Java Native Interface的缩写.它提供了若干的API实现了Java和其它语言 ...

  9. WinForm窗体键盘事件,支持方向键和回车键

    /// <summary> /// 快捷键操作 /// </summary> protected override bool ProcessCmdKey(ref Message ...

  10. 开源的报表系统easyreport的部署

    https://github.com/xianrendzw/EasyReport/blob/master/docs/manual/version2_0.md