Add to List 349. Intersection of Two Arrays
Given two arrays, write a function to compute their intersection.
Example:
Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].
Note:
- Each element in the result must be unique.
- The result can be in any order.
给出两个数组,求他们的交集。出题的意图何在?
public int[] intersection(int[] nums1, int[] nums2) {
Set<Integer> set = new HashSet<>();
Set<Integer> inter = new HashSet<>();
for(int a:nums1)
set.add(a);
for(int a:nums2)
{
if(set.contains(a))
inter.add(a);
}
int result[] = new int[inter.size()];
int i = 0;
for(int s:inter)
result[i++] = s;
return result;
}
思路简单,代码啰嗦,用python三行搞定
def intersection(self, nums1, nums2):
a = set(nums1)
b = set(nums2)
return list(a & b);
Add to List 349. Intersection of Two Arrays的更多相关文章
- [LeetCode] 349 Intersection of Two Arrays && 350 Intersection of Two Arrays II
这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/in ...
- 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 ...
- [LeetCode] 349. Intersection of Two Arrays 两个数组相交
Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...
- 349. Intersection of Two Arrays
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- 349. Intersection of Two Arrays 是否重复
不重复的: 方法一 [一句话思路]:排序之后用归并. [一刷]: 根据返回方法的类型来判断corner case.判断空.0数组的corner case还不一样,得分开写 由于先排序了,nums1[i ...
- [leetcode]349. Intersection of Two Arrays数组交集
Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...
- LeetCode 349. Intersection of Two Arrays (两个数组的相交)
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- 【LeetCode】349. Intersection of Two Arrays 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:Java解法,HashSet 方法二:Pyt ...
- LeetCode 349. Intersection of Two Arrays
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
随机推荐
- opencv摄像头捕获图像
#include <iostream> #include <opencv2/opencv.hpp> using namespace cv; using namespace st ...
- 如何搭建一个B2B电商的跨境系统网站?
国内的B2B跨境电商系统开发定制如何做才符合标准?商家怎么搭建专属的电商供应链系统?目前并不是大多数电商行业的公司可以应对得起组建团队来做,下面分享下大概的建设供应链商城网站思路和步骤(以数商云跨境电 ...
- cinder存储节点 后端采用lvm、nfs安装配置
#cinder存储节点 openstack pike 部署 目录汇总 http://www.cnblogs.com/elvi/p/7613861.html #cinder存储节点 #cinder后端采 ...
- Mysql实现企业级日志管理、备份与恢复实战
背景 随着业务的发展,公司业务和规模不断扩大,网站积累了大量的用户信息和数据,对于一家互联网公司来说,用户和业务数据是根基.一旦公司的数据错乱或者丢失,对于互联网公司而言就等于说是灭顶之灾,为防止系统 ...
- ChatterBot之linux下安装mongodb 02
当前环境 :centos 6.9 mongodb版本 mongodb-linux-x86_64-3.4.4.tgz 使用链接工具:studio-3t-x64.msi.zip 首先我们先来安装mongo ...
- Python 简单的输出
Python hw其实非常简单. 2 行代码 vi test.py [Python] 纯文本查看 复制代码 ? 1 2 #!/usr/bin/python print "Hello Worl ...
- Linux下SVN安装配置以及使用
第一章 安装 1. 采用源文件编译安装.源文件共两个,为: subversion-1.6.21.tar.gz(subversion 源文件) subversion-deps-1.6.21.tar.gz ...
- 利用nginx实现负载均衡和动静分离
1.Nginx介绍 Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器 . Nginx 是由 ...
- iOS 处理socket粘包问题
1.什么是粘包? 粘包通常出现在TCP的协议里面,对于UDP来说是不会出现粘包状况的,之所以出现这种状况的原因,涉及到一种名为Nagle的算法. Nagle算法通过减少必须发送的封包的数量,提高网络应 ...
- 5、C#基础 - C#的值类型
1.C#的值类型 有几个特点: 存储在栈里 基于值类型的变量直接包含值(值类型存储实际值). 将一个值类型变量赋给另一个值类型变量时,将复制包含的值. 这与引用类型变量的赋值不同,引用类型变量的赋值只 ...