Intersection Of Two ArraysII WayII
严氏北美IT公司面试真题汇总和解答论坛 :: LinkedIn公司面试真题: 注册用户可以看到隐藏题目:2017年下半年上机题,8 9 10三个月的onsite面试真题 :: 求2个list的交集系列题
Page 1 of 1
Intersection Of Two ArraysII WayII
public int[] intersect(int[] nums1, int[] nums2) {
Arrays.sort(nums1);
Arrays.sort(nums2);
ArrayList<Integer> list = new ArrayList<Integer>();
int p1=0, p2=0;
while(p1<nums1.length && p2<nums2.length){
if(nums1[p1]<nums2[p2]){
p1++;
}else if(nums1[p1]>nums2[p2]){
p2++;
}else{
list.add(nums1[p1]);
p1++;
p2++;
}
}
int[] result = new int[list.size()];
int i=0;
while(i<list.size()){
result[i]=list.get(i);
i++;
}
return result;
}
Arrays.sort(nums1);
Arrays.sort(nums2);
ArrayList<Integer> list = new ArrayList<Integer>();
int p1=0, p2=0;
while(p1<nums1.length && p2<nums2.length){
if(nums1[p1]<nums2[p2]){
p1++;
}else if(nums1[p1]>nums2[p2]){
p2++;
}else{
list.add(nums1[p1]);
p1++;
p2++;
}
}
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 WayI
» Intersection Of Two ArraysI WayII
» Intersection Of Two ArraysI WayI
» Intersection Of Two ArraysI WayII
» Intersection Of Two ArraysI WayI
严氏北美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