求两个sorted数组的intersection e.g. [1,2,3,4,5],[2,4,6] 结果是[2,4]
difference

类似merge, 分小于等于大于三种情况,然后时间O(m+n), 空间O(1)

 package ShortestSubsequenceIncluding;
import java.util.*; public class Solution2 {
public ArrayList<Integer> setInters(int[] arr1, int[] arr2) {
int i1=0, i2=0;
ArrayList<Integer> res = new ArrayList<Integer>();
while (i1<arr1.length && i2<arr2.length) {
if (arr1[i1] < arr2[i2]) i1++;
else if (arr1[i1] > arr2[i2]) i2++;
else {
res.add(arr1[i1]);
i1++;
i2++;
}
}
return res;
} public ArrayList<Integer> setDiff(int[] arr1, int[] arr2) {
int i1=0, i2=0;
ArrayList<Integer> res = new ArrayList<Integer>();
while (i1<arr1.length && i2<arr2.length) {
if (arr1[i1] < arr2[i2]) {
res.add(arr1[i1]);
i1++;
}
else if (arr1[i1] > arr2[i2]) {
i2++;
}
else {
i1++;
i2++;
}
}
while (i1 < arr1.length) {
res.add(arr1[i1]);
i1++;
}
return res;
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Solution2 sol = new Solution2();
ArrayList<Integer> res1 = sol.setInters(new int[]{1,2,3,4,4,5}, new int[]{2,4,4,6});
ArrayList<Integer> res2 = sol.setDiff(new int[]{2,3,4,4,4,5}, new int[]{1,2,4,4,6});
System.out.println(res2); } }

如果是无序的两个array,则Build两个hashMap, 遍历第一个map的keySet, O(m+n)time, O(m+n)space

G面经prepare: Set Intersection && Set Difference的更多相关文章

  1. python 并集union, 交集intersection, 差集difference, 对称差集symmetric_difference

    python的集合set和其他语言类似,是一个无序不重复元素集, 可用于消除重复元素. 支持union(联合), intersection(交), difference(差)和sysmmetric d ...

  2. 60-python基础-python3-集合-集合常用方法-交集、并集、差集、对称差集-intersection(&)-union(|)-difference(-)-symmetric_difference()

    交集.并集.差集-intersection(&)-union(|)-difference(-) 1-intersection(&) s1.intersection(s2),返回s1和s ...

  3. G面经prepare: set difference

    给你A{1,2,3,4,4,5}, B{2,4},求A-B={1,3,4,5},很简单. visit 1 只用一个HashMap package TwoSets; import java.util.* ...

  4. G面经prepare: Reorder String to make duplicates not consecutive

    字符串重新排列,让里面不能有相同字母在一起.比如aaabbb非法的,要让它变成ababab.给一种即可 Greedy: 跟FB面经Prepare task Schedule II很像,记录每个char ...

  5. G面经prepare: Maximum Subsequence in Another String's Order

    求string str1中含有string str2 order的 subsequence 的最小长度 DP做法:dp[i][j]定义为pattern对应到i位置,string对应到j位置时,shor ...

  6. G面经prepare: Pattern Match

    设定一个pattern 把 'internationalization' 变成 'i18n', 比如word是house,pattern可以是h3e, 3se, 5, 1o1s1等, 给pattern ...

  7. G面经prepare: Data Stream Average

    给一个datastream和一个fixed window size, 让我design一个class可以完成add number还有find average in the window. 就是不能用v ...

  8. G面经prepare: Android Phone Unlock Pattern

    1 2 3 4 5 6 7 8 9 只有中间没有其他键的两个键才能相连,比如1可以连 2 4 5 6 8 但不能连 3 7 9 但是如果中间键被使用了,那就可以连,比如5已经被使用了,那1就可以连9 ...

  9. G面经prepare: Jump Game Return to Original Place

    第二题 算法 给你一个arr 返回 T 或者 F arr的每个数代表从这个点开始跳几部,返回T的情况:从这个arr中任意一个数开始跳,可以在每个元素都跳到且只跳到一次的情况下返回到开始跳的元素 比如[ ...

随机推荐

  1. Address localhost:1099 is already in use 的错误

    http://blog.csdn.net/huazhongkejidaxuezpp/article/details/41813683

  2. 注册、启动、停止windows服务

    找到本机InstallUtil.exe命令 命令行下注册服务InstallUtil.exe D:\XXXXService.exe 启动服务 net start XXXXService 停止服务net ...

  3. Qt调用dll中的功能函数

    声明: 事先我已经自己动手写了一个简单的dll文件(myDLL.dll),C版接口的.并且用我前两篇有关DLL文章里面的方法,从dll中导出了导入库(.lib)文件,dll中有两个函数,原型如下:   ...

  4. CSS控制"标题前增加小图标或编号"

    ---题目前加图片--- p:before { content:url(xxx/xx.png); }//所有p的最前都有一个图标 p.a:after { content:url(xxx/xx.png) ...

  5. Object C语法学习

    #synthesize关键字: 根据@property设置,自动生成成员变量相应的存取方法,从而可以使用点操作符来方便的存取该成员变量 . @implementation 关键字,表明类的实现 @en ...

  6. UITextView打开文字中的URL

    1. 背景介绍 UITextView里显示的文字带有url,点击url可以打开对应的网页,可以分两种打开方式:(1)在App内打开url:(2)用safari打开url. 2. 实现代码: (1)声明 ...

  7. Celery - Best Practices

    If you've worked with Django at some point you probably had the need for some background processing ...

  8. Selenium2学习-018-WebUI自动化实战实例-016-自动化脚本编写过程中的登录验证码问题

    日常的 Web 网站开发的过程中,为提升登录安全或防止用户通过脚本进行黄牛操作(宇宙最贵铁皮天朝魔都的机动车牌照竞拍中),很多网站在登录的时候,添加了验证码验证,而且验证码的实现越来越复杂,对其进行脚 ...

  9. LintCode Subtree

    原题链接在这里:http://www.lintcode.com/en/problem/subtree/ You have two every large binary trees: T1, with ...

  10. 官方教程:教你用好微信JS-SDK接口

    微信开放JS-SDK接口,开发者和行业用户可谓是欢欣鼓舞.奔走相告,目测将激起一大波第三方开发商的创新产品!真真是H5开发者的利好!但也有用户表示,还不了解JS-SDK接口到底是啥,究竟怎么用.现在, ...