不重复的:

方法一

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

[一刷]:

  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. Linux命令详解-ftp服务器配置

    1.ftp服务器配置 1.ftp安装: rpm –qa | grep ftp 2.查看安装内容: rpm-ql |more 3.启动ftp服务: service vsftpd start 4.配置文件 ...

  2. 第3章 文件I/O(6)_高级文件操作:文件锁

    7. 高级文件操作:文件锁 (1)文件锁分类 分类依据 类型 说明 按功能分 共享读锁 文件描述符必须读打开 一个进程上了读锁,共它进程也可以上读锁进行读取 独占写锁 文件描述符必须写打开 一个进程上 ...

  3. php 学习笔记 设计和管理

    代码管理 文件路径.数据库名.密码禁止 hard coded 避免重复代码在多个页面复制粘贴 Gang of Four eXtreme Programming 的主要原则是坚决主张测试是项目成功的关键 ...

  4. ORM PetaPoco 框架的 CRUD 操作

    PetaPoco 的查询操作 public IEnumerable<T> GetAll(string sqlString, object[] obj) { try { IEnumerabl ...

  5. Js 动态设置DIV日期信息

    HTML代码如下: <div  id="time"> 2013年12月20日 14:49:02 星期五 </div> JS代码如下: window.onlo ...

  6. png-CRC32校验

    官方文档: https://www.w3.org/TR/PNG-CRCAppendix.html CRC32校验的数据,看原文 A four-byte CRC (Cyclic Redundancy C ...

  7. 本地git仓库常用操作

    SSH配置: 本机创建SSH key $ ssh-keygen -t rsa -C "youremail@example.com" 将SSHkey添加到git仓库:id_rsa.p ...

  8. 8.rem适配

    <!DOCTYPE html> <!--lang="en" : 英语 :声明当前页面的语言类型.--> <html lang="en&quo ...

  9. jpa 多对多

    entity   Item package entity; import java.util.HashSet; import java.util.Set; import javax.persisten ...

  10. 数据库中查询json 样式的值的sql语句

    参考:http://www.lnmp.cn/mysql-57-new-features-json.html 方式一: 可以查到json中的Key:value SELECT * FROM EDI.edi ...