当前位置:网站首页>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;
}
边栏推荐
- Calculate the number of days between two times (supports cross month and cross year)
- Hump naming and underlining
- Mysql database backup and restore:
- Conf/tongweb Functions of properties
- 顶部下滑沉浸式dialog
- powershell优化之一:提示符美化
- Unity game optimization [Second Edition] learning record 6
- Application virtual directory static resource configuration on tongweb
- Parallelgateway and exclusivegateway of 14 gateways
- How to Algorithm Evaluation Methods
猜你喜欢

Timeout thread log for tongweb

Feel the power of shardingsphere JDBC through the demo

Shardingsphere JDBC < bind table > avoid join Cartesian product

Source code analysis of ArrayList

AUTOSAR actual combat tutorial pdf version

Config server configuration center of Nacos series

Sentinel series integrates Nacos and realizes dynamic flow control

Problems encountered in the use of PgSQL

Nacos series registry principle and source code analysis

ZABBIX proxy, sender (without agent monitoring), performance optimization
随机推荐
JS output uincode code
2 first experience of drools
Error: unmapped character encoding GBK
JNDI configuration for tongweb7
设置自定义dialog的正确宽高
Working principle of sentinel series (concept)
Django uses redis to store sessions starting from 0
How to Algorithm Evaluation Methods
Cross compile HelloWorld with cmake
Explanation of service registration and discovery API of Nacos series
Four shardingsphere JDBC sharding strategies
Tongweb adapts to openrasp
August 15, 2021 another week
2021.9.30 learning log -postman
Misunderstanding of tongweb due to ease of use
Compilation croisée helloworld avec cmake
3. Postman easy to use
计算两个时间相差的天数(支持跨月、跨年)
Mysql database crud operation
16 the usertask of a flowable task includes task assignment, multi person countersignature, and dynamic forms