Intersection Of Two ArraysII WayI
严氏北美IT公司面试真题汇总和解答论坛 :: LinkedIn公司面试真题: 注册用户可以看到隐藏题目:2017年下半年上机题,8 9 10三个月的onsite面试真题 :: 求2个list的交集系列题
Page 1 of 1
Intersection Of Two ArraysII WayI
public int[] intersect(int[] nums1, int[] nums2) {
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for(int i: nums1){
if(map.containsKey(i)){
map.put(i, map.get(i)+1);
}else{
map.put(i, 1);
}
}
ArrayList<Integer> list = new ArrayList<Integer>();
for(int i: nums2){
if(map.containsKey(i)){
if(map.get(i)>1){
map.put(i, map.get(i)-1);
}else{
map.remove(i);
}
list.add(i);
}
}
int[] result = new int[list.size()];
int i =0;
while(i<list.size()){
result[i]=list.get(i);
i++;
}
return result;
}
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for(int i: nums1){
if(map.containsKey(i)){
map.put(i, map.get(i)+1);
}else{
map.put(i, 1);
}
}
ArrayList<Integer> list = new ArrayList<Integer>();
for(int i: nums2){
if(map.containsKey(i)){
if(map.get(i)>1){
map.put(i, map.get(i)-1);
}else{
map.remove(i);
}
list.add(i);
}
}
int[] result = new int[list.size()];
int i =0;
while(i<list.size()){
result[i]=list.get(i);
i++;
}
return result;
}
Similar topics
» Intersection Of Two ArraysII WayII
» Intersection Of Two ArraysI WayI
» Intersection Of Two ArraysI WayII
» Intersection Of Two ArraysI WayI
» Intersection Of Two ArraysI WayII
严氏北美IT公司面试真题汇总和解答论坛 :: LinkedIn公司面试真题: 注册用户可以看到隐藏题目:2017年下半年上机题,8 9 10三个月的onsite面试真题 :: 求2个list的交集系列题
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum