当前位置:网站首页>leetcode刷题:哈希表02 (两个数组的交集)
leetcode刷题:哈希表02 (两个数组的交集)
2022-06-23 17:23:00 【涛涛英语学不进去】
349. 两个数组的交集
题意:给定两个数组,编写一个函数来计算它们的交集。

说明:
输出结果中的每个元素一定是唯一的。
我们可以不考虑输出结果的顺序。
不难,先把一个数组去重,用set,再用一个表示所有数的标记数组,遍历第二个数组,看看每个元素是否被contains在第一个数组里,如果存在,则该数字的位置标记存在。最后再统计大小,返回结果数组。
package com.programmercarl.hashtable;
import java.util.HashSet;
/** * @ClassName Intersection * @Descriotion https://leetcode.cn/problems/intersection-of-two-arrays/ * @Author nitaotao * @Date 2022/6/20 14:32 * @Version 1.0 * 给定两个数组 nums1 和 nums2 ,返回 它们的交集 。输出结果中的每个元素一定是 唯一 的。我们可以 不考虑输出结果的顺序 。 * 两个数组的交集 **/
public class Intersection {
public static void main(String[] args) {
intersection(new int[]{
4, 9, 7,7,6,7}, new int[]{
5,0,0,6,1,6,2,2,4});
}
public static int[] intersection(int[] nums1, int[] nums2) {
//数组去重
HashSet<Integer> set1 = new HashSet<Integer>();
for (int i : nums1) {
set1.add(i);
}
//数的范围是 [0,1001]
int[] resultTemp = new int[1001];
for (int i : nums2) {
if (set1.contains(i)) {
//如果包含此数
resultTemp[i] += 1;
}
}
int count = 0;
//因为返回结果要的是数组,所以最后得转成数组
for (int i = 0; i < 1001; i++) {
if (resultTemp[i] != 0) {
count++;
}
}
int[] result = new int[count];
for (int i = 0, j = 0; i < 1001; i++) {
if (resultTemp[i] != 0) {
result[j] = i;
j++;
}
}
return result;
}
}

发现这个题之前做过
public int[] intersection(int[] nums1, int[] nums2) {
TreeSet<Integer> treeSet = new TreeSet();
for (int i = 0; i < nums1.length; i++) {
for (int j = 0; j < nums2.length; j++) {
if (nums1[i] == nums2[j]) {
treeSet.add(nums1[i]);
}
}
}
int[] intersection = new int[treeSet.size()];
Iterator<Integer> iterator = treeSet.iterator();
int index = 0;
while (iterator.hasNext()) {
intersection[index] = iterator.next();
index++;
}
return intersection;
}

六个月前做的,效率不如现在的,果然人还是在进步的。
之前思路是直接暴力两层遍历。如果相等,添加到set,再处理成需要返回的数组。
边栏推荐
- A set of code to launch seven golang web frameworks at the same time
- Ner's past, present and future Overview - Future
- SimpleDateFormat在多线程环境下存在线程安全问题。
- 12. Manage network environment
- Redis 集群
- The draganddrop framework, a new member of jetpack, greatly simplifies the development of drag and drop gestures!
- Establishment and use of SSL VPN (OpenVPN)
- January 5, 2022: there are four kinds of rhythms: AABB, ABAB and ABB
- [learning notes] tidb learning notes (III)
- New function! Qianfan magic pen apaas December capability monthly report
猜你喜欢

Paper reading (56):muti features predction of protein translational modification sites (task)

The battlefield of live broadcast e-commerce is not in the live broadcast room

论文阅读 (53):Universal Adversarial Perturbations

Paper reading (50):a novel matrix game with payoffs of maximal belt structure

Counter attack and defense (1): counter sample generation in image domain

【win10 VS2019 opencv4.6 配置参考】

建立自己的网站(13)

"Tribute to a century old master, collect pocket book tickets"

leetcode刷题:哈希表07 (三数之和)

TT 语音落地 Zadig:开源共创 Helm 接入场景,环境治理搞得定!
随机推荐
Nanny level teaching! Take you to play with time complexity and space complexity!
What does the science and technology interactive sand table gain popularity by virtue of
Redis cluster
Self taught programming introduction, what language to learn first?
console. Log() is an asynchronous operation???
[failure announcement] there is a problem with the redis that replaces memcached, causing the website to fail
Kerberoasting without SPN
uniapp项目中防止用户重复提交
QML type: Loader
[unity] instructions for beginners of textanimator plug-in
Goframe framework: add tracing Middleware
Reading papers (51):integration of a holonic organizational control architecture and multiobjective
【win10 VS2019 opencv4.6 配置参考】
Implementing Domain Driven Design - using ABP framework - repository
Paper reading (55):dynamic multi robot task allocation under uncertainty and temporary constraints
README
论文阅读 (47):DTFD-MIL: Double-Tier Feature Distillation Multiple Instance Learning for Histopathology..
What if the website is poisoned
Theory of technology that must be learned by chip manufacturers (4-1) clock technology and reset Technology
Troubleshooting and modification process of easycvr interface dislocation in small screen