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.*; 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的更多相关文章
- 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 ...
- G面经prepare: Reorder String to make duplicates not consecutive
字符串重新排列,让里面不能有相同字母在一起.比如aaabbb非法的,要让它变成ababab.给一种即可 Greedy: 跟FB面经Prepare task Schedule II很像,记录每个char ...
- G面经prepare: Maximum Subsequence in Another String's Order
求string str1中含有string str2 order的 subsequence 的最小长度 DP做法:dp[i][j]定义为pattern对应到i位置,string对应到j位置时,shor ...
- G面经prepare: Pattern Match
设定一个pattern 把 'internationalization' 变成 'i18n', 比如word是house,pattern可以是h3e, 3se, 5, 1o1s1等, 给pattern ...
- G面经prepare: Data Stream Average
给一个datastream和一个fixed window size, 让我design一个class可以完成add number还有find average in the window. 就是不能用v ...
- 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 ...
- G面经prepare: Jump Game Return to Original Place
第二题 算法 给你一个arr 返回 T 或者 F arr的每个数代表从这个点开始跳几部,返回T的情况:从这个arr中任意一个数开始跳,可以在每个元素都跳到且只跳到一次的情况下返回到开始跳的元素 比如[ ...
- 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 ...
- G面经Prepare: Valid Preorder traversal serialized String
求问下各位大神,怎么判断一个按照Preorder traversal serialized的binary tree的序列是否正确呢?不能deserialize成树比如 A) 9 3 4 # # 1 # ...
随机推荐
- Technical analysis of client identification mechanisms
http://www.chromium.org/Home/chromium-security/client-identification-mechanisms Chromium > Chro ...
- hitTest:WithEvent 和Responder Chain
这个方法是找到那个View被touch,当找到后就成为响应链的第一个了,如果他不能处理这个Event,那么就找nextResponder 直至application 如果不能处理,那就会丢弃掉. ht ...
- JDK核心包学习
StringBuffer 线程安全.可变字符序列 StringBuilder 非线程安全.可变字符序列,比StringBuffer更快 Boolean 使用valueOf产生Boolean实例 ...
- sqlserver 获取当前操作的数据库名称
Select Name From Master..SysDataBases Where DbId=(Select Dbid From Master..SysProcesses Where Spid = ...
- html代码转义到js时,往往会遇到问题,这代码实现html和js互转
这段代码是直接可以用的,大家不妨试试.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...
- SIP学习(实例详解)
本文摘自:http://blog.chinaunix.net/uid-20655530-id-1589483.html 学习 SIP 协议最快捷的方法是通过范例来学习, 找到了一个完整的呼叫流程,le ...
- spring.net使用
1.方法 using System; using System.Collections.Generic; using System.Linq; using System.Text; using Sys ...
- JavaWeb学习笔记(一)Mac 下配置Tomcat环境
最近,想鼓捣与服务器端的交互,只能自己搭建环境了. 上个周一鼓捣了一点,周五再鼓捣,发现忘得已经差不多了.好记性不如烂笔头,还是记录下来比较好. 首先,去Tomcat的官网,下载Mac版的Tomca ...
- FragmentActivity和Activity的区别
[转载] 直说总结了: 1.fragmentactivity 继承自activity,用来解决android3.0 之前没有fragment的api,所以在使用的时候需要导入support包,同时继承 ...
- ubuntu12.04 安装 setuptools
ubuntu 12.04 安装django时,提示缺少setuptools. 转载自: http://blog.csdn.net/xudongtiankong/article/details/8180 ...