求两个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. Spark 2.0

    Apache Spark 2.0: Faster, Easier, and Smarter http://blog.madhukaraphatak.com/categories/spark-two/ ...

  2. SQuirreL 连接 hive

    软件安装版本: hadoop-2.5.1 hbase-0.98.12.1-hadoop2 apache-hive-1.2.1-bin SQuirreL SQL Client3.7 集成步骤: 1. S ...

  3. C# 64位系统中类型所占空间大小

    Boolean   8Byte DateTime 8Byte Decimal  16Byte String 引用地址空间8Bypte Int 4Bypte 类所占空间大小 (byte):各个filed ...

  4. QT多线程及通过事件进行通信(通过自定义事件,然后QApplication::postEvent给主界面,我之前用的是信号槽)

    可以通过QThread实现跨平台的多线程开发,Qt库负责在特定平台上的特定多线程实现.要采用QThread进行多线程开发,首先需要包含头文件: #include <QThread> 然后需 ...

  5. 正则表达式lastIndex属性浅析

    有这样一段代码: var newDateStr = " 11 13:48:18"; var reg = new RegExp("[0-9]+","g& ...

  6. A Guide to Creating a Quality Project Schedule

    Successful projects start with a good quality project schedule. Creating a schedule is one of the fi ...

  7. POI对Excel

    完美兼容excel2003 和excel2007的读取,处理了所有excel所有的类型,依赖包如下: poi-3.10-FNAL.jar poi-ooxml-3.10-FNAL.jar poi-oox ...

  8. TortoiseGit 的使用

    日常用法 (1) 创建新库 在文件夹中按右键, 选择Git Create repository here 就可以创建库了. 在出现的窗口中, 不勾选选项, 直接按OK 在目录中就会出现一个名为.git ...

  9. php自定义错误处理和try{}catch(){}学习

    <?php //语法错误 //运行时的错误 //逻辑错误 //php的错误报告级别 // display_errors; // ini_set("display_errors" ...

  10. toggle笔记

    <!DOCTYPE html> <!-- saved from url=(0040)http://v3.bootcss.com/examples/carousel/ --> & ...