当前位置:网站首页>Leetcode- intersection of two arrays - simple
Leetcode- intersection of two arrays - simple
2022-06-13 05:47:00 【AnWenRen】
title :349 Intersection of two arrays - Simple
subject
Given two arrays , Write a function to calculate their intersection .
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
- Each element of the output must be unique .
- We can ignore the order of output results .
Code Java
public int[] intersection(int[] nums1, int[] nums2) {
Set result = new HashSet();
Set set = new HashSet();
for (int i = 0; i < nums1.length; i++) {
set.add(nums1[i]);
}
for (int i = 0; i < nums2.length; i++) {
if (set.contains(nums2[i])){
result.add(nums2[i]);
}
}
int[] re = new int[result.size()];
int j = 0;
for (Object o : result) {
re[j++] = (int) o;
}
return re;
}
边栏推荐
- Comment procéder à l'évaluation des algorithmes
- A simple recursion problem of linked list
- 2021.9.29 learning log restful architecture
- MySQL main query and sub query
- Automatic database backup (using Navicat)
- MySQL log management and master-slave replication
- Mysql database backup and restore:
- 2021.9.29学习日志-MIME类型
- NVIDIA Jetson nano/xavier NX capacity expansion tutorial
- Hainan University Postgraduate Entrance Examination electronic information (085400) landing experience
猜你喜欢
MySQL main query and sub query
Config server configuration center of Nacos series
Byte buddy print execution time and method link tracking
17 servicetask of flowable task
powershell优化之一:提示符美化
SPI primary key generation strategy for shardingsphere JDBC
Service architecture diagram of Nacos series
Mysql database crud operation
MySQL built-in functions
MySQL fuzzy query and sorting by matching degree
随机推荐
Unity game optimization (version 2) learning record 7
OpenGL马赛克(八)
AUTOSAR actual combat tutorial pdf version
Basic application of sentinel series
Hump naming and underlining
OpenGL mosaic (VIII)
Mobile end adaptation scheme
Pyqt5 module
Exception after repeated application redeployment on tongweb: application instance has been stopped already or outofmemoryerror:metaspace
Input a number and output each digit from high to low
Source code analysis of ArrayList
NVIDIA Jetson Nano/Xavier NX 扩容教程
3. Postman easy to use
Basic operations of MySQL auto correlation query
[China & some provinces and cities] JSON file for offline map visualization
Nacos series registry principle and source code analysis
The 13th week of the second semester of sophomore year
Tongweb crawl performance log script
为什么那么多人讨厌A-Spice
Summary of the 11th week of sophomore year