您的位置:首页 > 其它

Intersection of Two Arrays&Intersection of Two Arrays II

2016-06-13 21:29 459 查看
Intersection of Two Arrays

Given two arrays, write a function to compute their intersection.

Example:

Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].

Note:

Each element in the result must be unique.

The result can be in any order.

JS代码:

var intersection = function(nums1, nums2) {
var intersect=[];
// var k=0;
// var j=0,i=0;
// if(nums1.length===0||nums2.length===0)
// {
//     return intersect;
// }
// nums1.sort(function(a,b){  //数组升序排列
//     return a-b;
// });
// nums2.sort(function(a,b){  //数组升序排列
//     return a-b;
// });

//     while((j<nums1.length)&&(i<nums2.length))
//     {
//         while(nums2[i]<=nums1[j])
//       {

//         if(nums2[i]===nums1[j])
//         {
//             if(k===0)
//             {
//                 intersect[0]=nums2[i];
//                     k++;
//                     i++;
//                     j++;
//           }
//           else{
//                   if(nums2[i]!==intersect[k-1])
//                 {
//                     intersect[k]=nums2[i];
//                     k++;

//                 }
//                  i++;
//               j++;
//           }

//         }else {
//           i++;     //若nums2[i]<nums1[j],则增大nums2[i]
//         }

//     }
//      j++;   //若nums2[i]>nums1[j],则增大nums2[j]
//     }
if(nums1===null||nums2===null)
{
return null;
}
var num1Obj={};
var num2Obj={};
var len1=nums1.length;
var len2=nums2.length;
for(var i=0;i<len1;i++)//对象num1Obj存储数组num1中的值
{
if(!num1Obj.hasOwnProperty(nums1[i]))
{
num1Obj[nums1[i]]=1;
}else{
num1Obj[nums1[i]]++;
}
}
for(var j=0;j<len2;j++)//对象num2Obj存储数组num2中的值
{
if(!num2Obj.hasOwnProperty(nums2[j]))
{
num2Obj[nums2[j]]=1;
}else{
num2Obj[nums2[j]]++;
}
}
for(var k in num1Obj)
{
if(num2Obj.hasOwnProperty(k))
{
intersect.push(parseInt(k));
}
}
return intersect;
};


2.Intersection of Two Arrays II

Given two arrays, write a function to compute their intersection.

Example:

Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2].

Note:

Each element in the result should appear as many times as it shows in both arrays.

The result can be in any order.

Follow up:

What if the given array is already sorted? How would you optimize your algorithm?

What if nums1’s size is small compared to nums2’s size? Which algorithm is better?

What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?

public class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
Map<Integer, Integer> map1 = new HashMap<Integer, Integer>();
for (int num : nums1) {
if (map1.containsKey(num)) {
map1.put(num, map1.get(num) + 1);
} else {
map1.put(num, 1);
}
}

Map<Integer, Integer> map2 = new HashMap<Integer, Integer>();
for (int num : nums2) {
if (map2.containsKey(num)) {
map2.put(num, map2.get(num) + 1);
} else {
map2.put(num, 1);
}
}

List<Integer> list = new ArrayList<Integer>();
for (Map.Entry<Integer, Integer> entry : map1.entrySet()) {
int times = entry.getValue();
if (map2.containsKey(entry.getKey())) {
times = Math.min(times, map2.get(entry.getKey()));
} else {
times = 0;
}
for (int i = 0; i < times; i++) {
list.add(entry.getKey());
}
}

int[] res = new int[list.size()];
for (int i = 0; i < list.size(); i++) {
res[i] = list.get(i);
}
return res;
}
}


总结:做这两个算法题最大的收获就是学会了一种时间复杂度为O(n)排序的算法——计数排序算法。

计数排序算法:

以下是我在别人博客上看到的相关解释,通俗易懂,所以粘贴于此。

不过实际上,在数字范围有限制的情况下,(必须是数字才行的吧)是有一个这样的算法的,只需要用一个数组记录每个数字出现次数就可以了。

假定你的数字范围在0到65535范围之内,定义一个数组count[65536](这个空间是常量,和n无关,所以是O(1) ),初值全部为0。

那么假设有下面这些数字:

100

200

300

119

0

6



那么对于每个这个数字,都做在count中记录一下:

100 => count[100]++

200 => count[200]++

300 => count[300]++

119 => count[119]++

0 => count[0]++

6 => count[6]++



最后,遍历一边所有这些数字就可得到0~65535每个数字的个数(在count数组中),然后再顺序遍历count数组,count
= m,则输出m个n,(比如说有count[3] = 2, 那么说明有2个数字3),依次输出,最后可得结果。第一次遍历是O(n),第二次遍历是O(1),为常量,所以最后的时间复杂度为O(n),而空间复杂度为O(1)

这个算法很简单,相信大家都会,只是这个题太过于变态了,一般会把面试者吓住
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数组交叉