当前位置:网站首页>LeetCode 349. Intersection of two arrays (simple, array) Day12
LeetCode 349. Intersection of two arrays (simple, array) Day12
2022-04-23 02:05:00 【White Code】
Title Description : Given two arrays nums1 and nums2 , return Their intersection . Every element in the output must be only Of . We can Regardless of the order of the output results .
Example 1:
Input :nums1 = [1,2,2,1], nums2 = [2,2]
Output :[2]
Example 2:
Input :nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output :[9,4]
explain :[4,9] It is also passable
Tips :
1 <= nums1.length, nums2.length <= 1000
0 <= nums1[i], nums2[i] <= 1000
Conclusion ideas :
Method 1 : Sort + Double pointer
1、 Let me declare one List aggregate , Used to store the same elements in two arrays , The reason for not using arrays is that the array length is fixed , I don't know how many elements are the same , So it's best to use collections ;
2、 First sort the two sets ;
3、 Traverse two arrays to find the same element , Add the same elements to the collection , But before adding to the collection, you must judge whether the element already exists in the collection , If it already exists, discard the addition ;
4、 Add elements from the collection to the array , Return the array .
Method 2 : Use HashSet aggregate
1、 Declare two Set aggregate ,set1, set2(Set The basic characteristic of a set is disorder , No index , Do not repeat );
2、 Put the array nums1 Add elements from to set1 Collection ;
3、 Traversal array nums2, Judge nums2 Whether the elements in set1 Exists in collection , If present, add to set2 Collection , Otherwise, discard the element , Until the end of the traversal ;
4、 hold set2 The elements in the collection can be copied into the array , Last return array .
If you want to know more about Collection In a collection system Set and List aggregate , You can refer to the article :https://blog.csdn.net/qq_43751200/article/details/123803851
Code implementation :
public class Intersection {
public static void main(String[] args) {
int[] nums1 = {4,9,5};
int[] nums2 = {9,4,9,8,4};
System.out.println(Arrays.toString(intersection1(nums1, nums2)));
}
// Method 1: Sort + Double pointer
public static int[] intersection(int[] nums1, int[] nums2) {
// 1. Make a statement List aggregate
List<Integer> list = new ArrayList<>();
// 2. An array nums1, nums2 Sort
Arrays.sort(nums1);
Arrays.sort(nums2);
// 3. Traverse the array to find the same element
int m = 0, n = 0;
while (m < nums1.length && n < nums2.length){
if(nums1[m] == nums2[n]){
if(list.contains(nums1[m])){
// If it already exists in the collection , There's nothing to do
}else {
list.add(nums1[m]);
}
// The pointer needs to move forward
m++;
n++;
}else if(nums1[m] > nums2[n]){
n++;
}else {
m++;
}
}
// 4. hold list Convert array to array
int[] nums = new int[list.size()];
for(int i = 0; i < nums.length; i++){
nums[i] = list.get(i);
}
return nums;
}
// Method 2:Set aggregate
public static int[] intersection1(int[] nums1, int[] nums2) {
Set<Integer> set1 = new HashSet<>(); // HashSet characteristic : disorder 、 No repetition 、 No index .
Set<Integer> set2 = new HashSet<>();
for(int i = 0; i < nums1.length; i++){
set1.add(nums1[i]);
}
for(int i = 0; i < nums2.length; i++){
if(set1.contains(nums2[i])){
set2.add(nums2[i]);
}
}
int[] nums = new int[set2.size()];
int k = 0;
for(Integer it : set2){
nums[k++] = it;
}
return nums;
}
}
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/intersection-of-two-arrays
版权声明
本文为[White Code]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220839089303.html
边栏推荐
- ThinkPHP kernel development blind box mall source code v2 0 docking easy payment / Alibaba cloud SMS / qiniu cloud storage
- BGP服务器在什么业务场景会被用到?
- How to set computer IP?
- 001_redis设置存活时间
- Use of j-link RTT
- Numerical remapping method (remap)
- 中金财富是国企吗,开户安全吗
- Shardingsphere sub database and sub table
- Uncover floating-point operations hidden by the ARM compiler
- Is CICC fortune a state-owned enterprise and is it safe to open an account
猜你喜欢

What is a dial-up server and what is its use?

浅析一下隧道代理IP的优缺点。

How to choose a good dial-up server?

Micro build low code zero foundation introductory course

Esp32 message queue using FreeRTOS

Campus transfer second-hand market source code
![[hands on learning] network depth v2.1 Sequence model](/img/51/0de4c7972a99151007a8f27f351c83.png)
[hands on learning] network depth v2.1 Sequence model

搭建网站是用物理机还是云主机好?

用TensorFlow实现线性回归(包括过程中出现的问题及解决方法)

Thinkphp内核开发盲盒商城源码v2.0 对接易支付/阿里云短信/七牛云存储
随机推荐
配置iptables实现本地端口转发的方法详解
Leetcode46 Full Permutation
中金财富是国企吗,开户安全吗
Startup of openstack service
Communication summary between MCU and 4G module (EC20)
openstack 服务的启动
Unicorn bio raised $3.2 million to turn prototype equipment used to grow meat into commercial products
Micro build low code zero foundation introductory course
89 régression logistique prédiction de la réponse de l'utilisateur à l'image de l'utilisateur
What should I pay attention to when using proxy IP?
如何“优雅”的测量系统性能
CC2541的仿真器CC Debugger使用教程
010_StringRedisTemplate
Introduction to micro build low code zero Foundation (lesson 2)
009_Redis_RedisTemplate入门
leetcode:27. 移除元素【count remove小操作】
从开源爱好者到 Apache 董事,一共分几步?
Some tips for using proxy IP.
2018 China Collegiate Programming Contest - Guilin Site J. stone game
有哪些常见的代理ip问题?