给你A{1,2,3,4,4,5}, B{2,4},求A-B={1,3,4,5},很简单. visit 1

只用一个HashMap

 package TwoSets;
import java.util.*; public class Solution {
public ArrayList<Integer> findDiff(int[] arr1, int[] arr2) {
ArrayList<Integer> res = new ArrayList<Integer>();
HashMap<Integer, Integer> m1 = new HashMap<Integer, Integer>();
for (int item1 : arr1) {
if (!m1.containsKey(item1)) {
m1.put(item1, 1);
}
else {
m1.put(item1, m1.get(item1)+1);
}
}
for (int item2 : arr2) {
if (m1.containsKey(item2)) {
m1.put(item2, m1.get(item2)-1);
}
if (m1.get(item2) == 0) m1.remove(item2);
}
for (int elem : m1.keySet()) {
int num = m1.get(elem);
while (num > 0) {
res.add(elem);
num--;
}
}
return res;
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Solution sol = new Solution();
ArrayList<Integer> res = sol.findDiff(new int[]{1,2,3,4,4,5}, new int[]{2,4});
System.out.println(res);
} }

G面经prepare: set difference的更多相关文章

  1. G面经prepare: Set Intersection && Set Difference

    求两个sorted数组的intersection e.g. [1,2,3,4,5],[2,4,6] 结果是[2,4] difference 类似merge, 分小于等于大于三种情况,然后时间O(m+n ...

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

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

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

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

  4. G面经prepare: Pattern Match

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

  5. G面经prepare: Data Stream Average

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

  6. 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 ...

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

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

  8. G面经prepare: Sort String Based On Another

    Given a sorting order string, sort the input string based on the given sorting order string. Ex sort ...

  9. G面经Prepare: Valid Preorder traversal serialized String

    求问下各位大神,怎么判断一个按照Preorder traversal serialized的binary tree的序列是否正确呢?不能deserialize成树比如 A) 9 3 4 # # 1 # ...

随机推荐

  1. MVC问题

    将 <customErrors mode="Off" defaultRedirect="~/404.html">   mode 设置为off  就可 ...

  2. 在fortran下进行openmp并行计算编程

    最近写水动力的程序,体系太大,必须用并行才能算的动,无奈只好找了并行编程的资料学习了.我想我没有必要在博客里开一个什么并行编程的教程之类,因为网上到处都是,我就随手记点重要的笔记吧.这里主要是open ...

  3. 在Delphi中如何控制其它应用程序窗口

    在编写Delphi的应用程序中,常常涉及对其它Windows应用程序的操作.例如,在数据库的管理系统中,财务人员需要使用计算器,即可调用Windows内含的计算器功能,若每次使用,均通过“开始/程序/ ...

  4. 蓝牙4.0的LM层说明

    1.概念 The Link Manager Protocol (LMP) is used to control and negotiate all aspects of the operation o ...

  5. Abstract Algebra chapter 7

    7.7:Encrypt each of the following RSA messages x so that x is divided into blocks of integers of len ...

  6. FW Docker为容器分配指定物理网段的静态IP

    官方有关于网桥和IP配置的文档地址:https://docs.docker.com/articles/networking/ 1.宿主机(系统采用ubuntu-14.04.1-server-amd64 ...

  7. ngios

    一.Nagios简介 Nagios是一款开源的电脑系统和网络监视工具,能有效监控Windows.Linux和Unix的主机状态,交换机路由器等网络设置,打印机等.在系统或服务状态异常时发出邮件或短信报 ...

  8. 【android学习2】:Eclipse中HttpServlet类找不到

    Eclipse中使用的HttpServlet类之所以识别不到的原因是没有导入Servlet-api.jar包,这个包在所安装在的tomcat的lib文件下,所以只需要导入即可. 在需要导入的工程上右键 ...

  9. 修改PHP的memory_limit限制

    在运行PHP程序,通常会遇到“Fatal Error: Allowed memory size of xxxxxx bytes exhausted”的错误, 这个意味着PHP脚本使用了过多的内存,并超 ...

  10. Android 开发-Intent传递普通字符串

    假设A传递id到B中 ActivityA: Intent intent=new Intent();    intent.setClass(ActivityA.this,ActivityB.class) ...