349. Intersection of Two Arrays
2016-05-31 08:17
399 查看
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.
给两个数组,写个函数求出两者的交叉项。
注意:
结果中的元素必须是独立的,无序。
Python answer:
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.
给两个数组,写个函数求出两者的交叉项。
注意:
结果中的元素必须是独立的,无序。
Python answer:
class Solution(object): def intersection(self, nums1, nums2): """ :type nums1: List[int] :type nums2: List[int] :rtype: List[int] """ nums = [] for eachnum in nums1: if eachnum in nums2: nums2.remove(eachnum) nums.append(eachnum) return list(set(nums)) #常用列表去重方法
相关文章推荐
- Nodejs之MEAN栈开发(一)---- 路由与控制器
- JavaScript 的垃圾回收与内存泄露
- PAT 甲级 1024 Palindromic Number
- PAT 甲级 1024 Palindromic Number
- PAT 甲级 1023 Have Fun with Numbers
- PAT 甲级 1023 Have Fun with Numbers
- Ubuntu zip tar tar.gz tar.bz2
- Java基础之类与对象
- 我是菜鸟之--关于在Firefox与IE中捕捉回车事件的JavaScript方法
- ViewPager轮播
- 第二冲刺阶段——站立会议第七天5月31日
- 第二冲刺阶段——个人工作总结07
- 专题三总结
- Centos6.5 Jdk+Tomcat+Mysql环境安装
- 反删除工具合集
- Java连接MYSQL 数据库的连接步骤
- 互联网小知识
- 二叉树
- 字典树(Trie树)
- 一句话评论设计模式六大原则