20170403-leetcode-349/50-Intersection of Two Arrays.py
来源:互联网 发布:java封装snmp协议 编辑:程序博客网 时间:2024/06/07 05:24
两个同类型的题:349/350. 349:1、2;350:3,4
1.Description
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.
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.
解读
求两个数组的交集
2.Solution
变为set,求交集,再变为list
class Solution(object): def intersection(self, nums1, nums2): """ :type nums1: List[int] :type nums2: List[int] :rtype: List[int] """ return list(set(nums1) & set(nums2))
3.Intersection of Two Arrays II
Given two arrays, write a function to compute their intersection.
Example:
Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2].
Note:
Each element in the result should appear as many times as it shows in both arrays.The result can be in any order.
Follow up:
What if the given array is already sorted? How would you optimize your algorithm?What if nums1's size is small compared to nums2's size? Which algorithm is better?What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?
Given two arrays, write a function to compute their intersection.
Example:
Given nums1 = [1, 2, 2, 1]
, nums2 = [2, 2]
, return [2, 2]
.
Note:
- Each element in the result should appear as many times as it shows in both arrays.
- The result can be in any order.
Follow up:
- What if the given array is already sorted? How would you optimize your algorithm?
- What if nums1’s size is small compared to nums2’s size? Which algorithm is better?
- What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?
https://leetcode.com/problems/intersection-of-two-arrays-ii/#/description
解读
这次要求不去重复,有多少就找多少
4.Solution
方法:仍然先求集合的并集,找到公共元素,然后遍历这些元素找到他们在两个数组中的个数,取较小的值,添加到结果中
def intersect(self, nums1, nums2): dict1 = collections.Counter(nums1) dict2 = collections.Counter(nums2 result=[] for num in set(nums1) & set(nums2): result += [num] * (min(dict1[num], dict2[num])) return result
mark:collection.Counter(list)的返回值是字典类型
- 20170403-leetcode-349/50-Intersection of Two Arrays.py
- Leetcode-349 Intersection of Two Arrays
- leetcode 349 Intersection of Two Arrays C++
- LeetCode 349 Intersection of Two Arrays
- LeetCode-349&350.Intersection of Two Arrays
- LeetCode-349 Intersection of Two Arrays
- leetcode-Intersection of Two Arrays-349
- LeetCode 349 Intersection of Two Arrays
- LeetCode#349 Intersection of Two Arrays
- LeetCode(349)Intersection of Two Arrays
- [LeetCode]349 Intersection of Two Arrays
- [LeetCode-349] Intersection of Two Arrays(java)
- Leetcode 349 Intersection of Two Arrays
- LeetCode No.349 Intersection of Two Arrays
- leetcode-349-Intersection of Two Arrays
- [Leetcode 349]Intersection of Two Arrays
- leetcode 349 Intersection of Two Arrays
- Leetcode #349 Intersection of Two Arrays
- 【学堂在线数据挖掘:理论方法笔记】第九天(4.3)
- JAVA中整数类型数据溢出问题研究
- 开始写博客!
- 设计模式
- 题目541最强DE 战斗力
- 20170403-leetcode-349/50-Intersection of Two Arrays.py
- 验证python3实现单例模式的几种方法
- 安卓逆向系列教程 4.6 去广告
- Codeforces Round #369 (Div. 2) C. Coloring Trees —— DP
- 基于IB(Interactive Brokers)盈透证券的股票及期货行情获取讲解
- 【p1101-单词方阵】解题记录
- 如何自制微信小视频发朋友圈
- android listview
- Centos7 中安装haroopad