不重复的:

方法一

[一句话思路]:排序之后用归并。

[一刷]:

  1. 根据返回方法的类型来判断corner case。判断空、0数组的corner case还不一样,得分开写
  2. 由于先排序了,nums1[i] < nums2[j]时,i++可以继续在nums2[j]里找是否有相同的
  3. index=0时肯定不会重复,一定要插入
  4. 写法是Arrays.sort(nums1)
  5. 定义int i还不够,要初始化为0
  6. 并列用几个if可能会越界,还是用else if吧
  7. result定义的长度不能是temp.length=nums1,应该是index,实际统计了多少是多少
  8. 不直接返回temp[k]的原因:会有很多空

[二刷]:

  1. 没有定义int k就直接用了,注意要定义成为int型再用
  2. while (i < nums1.length && j < nums2.length) 两个边界要同时满足才行

格式上:for循环, else 后面也要加花括号

[总结]:结果中有几个数要看index加到了多少

[复杂度]:sort n*logn, merge n/1

[英文数据结构]:Array sort, array merge

[其他解法]:下面

[题目变变变]:array merge(后插)

class Solution {
public int[] intersection(int[] nums1, int[] nums2) { int[] zeroArray = new int[0];
if (nums1.length == 0 || nums2.length == 0) {
return zeroArray;
}
if (nums1 == null || nums2 == null) {
return null;
} Arrays.sort(nums1);
Arrays.sort(nums2); int i = 0;
int j = 0;
int index = 0;
int []temp = new int[nums2.length];//大小随意,反正result的大小只和实际的index大小有关 while (i < nums1.length && j < nums2.length) {
if (nums1[i] == nums2[j]) {
if (index == 0 || temp[index - 1] != nums1[i]) {
temp[index++] = nums1[i];
}
i++;
j++;
}
else if (nums1[i] < nums2[j]) {
i++;
}
else {
j++;
}
}//某一个数组到头了就不用管了,因为反正只需要比较相同的部分 int []result = new int[index];
for (int k = 0; k < index; k++) {
result[k] = temp[k];
} return result;
}
}

方法二

[一句话思路]:用两个HashSet,不重复地插入,不重复地查找:错。把nums1全部插入,用hashset.contains查找就行了

[一刷]:

  1. HashSet数据结构这样写
  2. hash.size可以测量hash的长度,用来衡量result的尺寸
  3. zeroArray的写法符合loweCamelCase
  4. for (Integer num : resultHash),打印resulthash中的全部。传到result时,还是要用index一个个地传(右边等于num)
for (Integer num : resulthash) {
result[index++] = num;
}

[总结]:注意表达方式:接口 名=new 类 。此类为HashSet<Integer>,大小写间隔

[复杂度]:n/n

[英文数据结构]:hashset

[其他解法]:见下

[题目变变变]:

class Solution {
public int[] intersection(int[] nums1, int[] nums2) { int[] zeroArray = new int[0];
if (nums1.length == 0 || nums2.length == 0) {
return zeroArray;
}
if (nums1 == null || nums2 == null) {
return null;
} HashSet<Integer> hash = new HashSet<>();
for (int i = 0; i < nums1.length; i++) {
hash.add(nums1[i]);
} HashSet<Integer> resulthash = new HashSet<>();
int j = 0;
for (j = 0; j < nums2.length; j++) {
if (hash.contains(nums2[j]) && !resulthash.contains(nums2[j])) {
resulthash.add(nums2[j]);
}
} int size = resulthash.size();
int[] result = new int[size];
int index = 0;
for (Integer num : resulthash) {
result[index++] = num;
} return result;
}
}

方法三:

[一句话思路]:nums2有重复就添加一次,v减100直到小于零。

[一刷]:

  1. 临时结果不能用hashset来存,因为元素不能重复。临时结果用list。好处:能且只能用get put方法,不能用下标访问
  2. 方法写错了,hashset hashmap区别:

HashSet和HashMap的区别

*HashMap* *HashSet*
HashMap实现了Map接口 HashSet实现了Set接口
HashMap储存键值对 HashSet仅仅存储对象
使用put()方法将元素放入map中 使用add()方法将元素放入set中
HashMap中使用键对象来计算hashcode值 HashSet使用成员对象来计算hashcode值,对于两个对象来说hashcode可能相同,所以equals()方法用来判断对象的相等性,如果两个对象不同的话,那么返回false
HashMap比较快,因为是使用唯一的键来获取对象 HashSet较HashMap来说比较慢

[总结]:一定要每次-100后判断是否还>0,一一对应

[复杂度]:

||获取|查找|添加/删除|空间||
|ArrayList |O(1)| O(1)|O(N) | O(N)|
|LinkedList | O(N) | O(N) |O(1)|O(N)
|HashMap | O(N/Bucket_size)|O(N/Bucket_size)|O(N/Bucket_size)|O(N)

[英文数据结构]:

[其他解法]:

[题目变变变]:

class Solution {
public int[] intersect(int[] nums1, int[] nums2) { Map<Integer, Integer> map = new HashMap<Integer, Integer>(); for (int i = 0; i < nums1.length; i++) {
if (!map.containsKey(nums1[i])) {
map.put(nums1[i], 1);
}
else if (map.containsKey(nums1[i])) {
map.put(nums1[i], map.get(nums1[i]) + 100);
}
} List<Integer> temp = new ArrayList<Integer>(); for (int j = 0; j < nums2.length; j++) {
if (map.containsKey(nums2[j]) && map.get(nums2[j]) > 0) {
temp.add(nums2[j]);
map.put(nums2[j], map.get(nums2[j]) - 100);
}
} int[] result = new int[temp.size()];
for (int k = 0;k < temp.size(); k++) {
result[k] = temp.get(k);
} return result;
}
}

349. Intersection of Two Arrays 是否重复的更多相关文章

  1. [LeetCode] 349 Intersection of Two Arrays && 350 Intersection of Two Arrays II

    这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/in ...

  2. LeetCode Javascript实现 283. Move Zeroes 349. Intersection of Two Arrays 237. Delete Node in a Linked List

    283. Move Zeroes var moveZeroes = function(nums) { var num1=0,num2=1; while(num1!=num2){ nums.forEac ...

  3. [LeetCode] 349. Intersection of Two Arrays 两个数组相交

    Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...

  4. 【leetcode】349. Intersection of Two Arrays

    Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, 2, 1 ...

  5. 【一天一道LeetCode】#349. Intersection of Two Arrays

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...

  6. LeetCode 349. Intersection of Two Arrays (两个数组的相交)

    Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...

  7. 【LeetCode】349. Intersection of Two Arrays 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:Java解法,HashSet 方法二:Pyt ...

  8. LeetCode 349. Intersection of Two Arrays

    Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...

  9. 349. Intersection of Two Arrays

    Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...

随机推荐

  1. Scrapy爬取人人网

    Scrapy发送Post请求 防止爬虫被反主要有以下几个策略 动态设置User-Agent(随机切换User-Agent,模拟不同用户的浏览器信息) 禁用Cookies(也就是不启用cookies m ...

  2. NOIP考前复习-数制转换,数论模板与文件读写

    数制转换有两种题型,一般一题,分值1.5分. 题型一:R进制转十进制 解法就是:按权展开,但要注意各个位的权,最低位(最右边)的权是0次方,权值为1. 纯整数的情况: (11010110)2 = 1× ...

  3. 5S后返回首页

    <!DOCTYPE html> <html> <head> <title>5S后返回首页</title> <meta http-equ ...

  4. K-means算法(理论+opencv实现)

    写在前面:之前想分类图像的时候有看过k-means算法,当时一知半解的去使用,不懂原理不懂使用规则...显然最后失败了,然后看了<机器学习>这本书对k-means算法有了理论的认识,现在通 ...

  5. linux双网卡绑定实现冗余与负载均衡

    1 编辑/etc/modprobe.conf   在/etc/modprobe.conf里加入如下两行: alias bond0 bonding options bond0 mode=1 miimon ...

  6. windows10系统telnet登陆

    参考网站:https://jingyan.baidu.com/article/acf728fd498e9ff8e510a322.html windows10系统以及没有telnet服务器端了. 需要单 ...

  7. 1.获取服务器IP、端口等

    比如,页面内部有一个连接,完整的路径应该是 http://192.168.0.1:8080/myblog/authen/login.do 其中http://server/是服务器的基本路径,myblo ...

  8. 文字折行不折行 css

    white-space : 1. normal  默认值 ,文字自动换行.               2. pre 使用<pre>标签形式,表示元素.                 * ...

  9. ubuntu16.04 dpkg强制安装 teamviewer

    dpkg遇到安装有依赖,而依赖的包有无法安装的时候,可以试试强制安装: .90154_amd64.deb 虽然报错,但是安装后还是可以使用. 如果使用: .90154_amd64.deb 提示下面错误 ...

  10. protocol_link

    蔡燧林:1992—2000年教育部考试中心研究生数学命题组组长现在退休养老.要想办法弄到他编的书(ps:别问怎么弄到,我和我同学都能弄到,你怎么会不能弄到呢)李林:目前在导航独家授课,他能屡屡命中考研 ...