(LeetCode)Intersection of Two Arrays --- 求交集
来源:互联网 发布:arp查看mac地址 编辑:程序博客网 时间:2024/06/10 03:22
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.
Subscribe to see which companies asked this question
解题分析:
第一次用python写交集,本打算还用C/C++的方法,使用哈希。
python中的话可以使用set函数来去重。
代码一:
__author__ = 'jiuzhang'# -*- coding:UTF-8 -*-class Solution(object): def intersection(self, nums1, nums2): return list(set(nums1) & set(nums2))
代码二:
# -*- coding: UTF-8 -*-class Solution(object): def intersection(self, nums1, nums2): set1 = set(nums1) ans = [] for x in nums2: if x in set1: ans += x set1.remove(x) return ans
0 0
- (LeetCode)Intersection of Two Arrays --- 求交集
- (LeetCode)Intersection of Two Arrays II --- 求交集,不去重
- Intersection of Two Arrays II--求交集
- leetcode-350 Intersection of Two Arrays II 求两个数组的交集 面试见到了吗?
- leetcode—350. Intersection of Two Arrays II 求两个list的交集
- leetcode解题之349 & 350. Intersection of Two Arrays Java版(求数组交集))
- [LeetCode]350. Intersection of Two Arrays II(求两个数组交集 II)
- Leetcode:350. Intersection of Two Arrays II 求两个数组的交集
- Intersection of Two Arrays--求两个向量的交集
- LeetCode 349. Intersection of Two Arrays(数组交集)
- LeetCode 350. Intersection of Two Arrays II(数组交集)
- 349.leetcode Intersection of Two Arrays(easy)[数组 交集 set]
- [LeetCode] Intersection of Two Arrays 两个数组的交集
- [LeetCode]349. Intersection of Two Arrays(两数组的交集)
- Leetcode :Intersection of Two Arrays 两个数组的交集
- leetcode 349. Intersection of Two Arrays 数组的交集
- leetcode-349. Intersection of Two Arrays 求两个数列的交集,list定义,set数据结构,list插入
- [LeetCode] Intersection of Two Arrays
- Android Studio系列教程二--基本设置与运行
- ARM学习步骤
- JDK,JRE,JVM区别与联系
- Initializing Java Tooling
- 浅谈C# 多态的魅力(虚方法,抽象,接口实现)
- (LeetCode)Intersection of Two Arrays --- 求交集
- 万网 弹性Web托管 提示No input file specified.
- 修改字符串字面量&用数组/指针实现的字符串。
- hibernate.hbm2ddl.auto为update无法自动建表
- Mybatis深入了解(四)----输入输出映射
- CodeBlocks快捷键
- XXX_$$_javassist_5 cannot be cast to javassist.util.proxy.ProxyObject
- Sublime Text 3 搭建C/C++环境 FOR ACM
- Sublime Text 3 安装插件