Leetcode with Python -> Sort
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.
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))
# for two set, set1&set2 makes a new set containing the intersection of these two sets,likewise, | makes the union set, and ^ makes the union set with the exception of the intersection elements
# this solution is clear but not fast
350. 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?
class Solution(object):
def intersect(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: List[int]
"""
from collections import Counter
c1=Counter(nums1)
c2=Counter(nums2)
return sum([[num]*min(c1[num],c2[num]) for num in c1&c2],[]) # sum([[1,2],[3,4]],[]) makes [1,2,3,4], why???
# Counter(some_list) makes a diction, whose key is the elements of the list and the value is the time it appears in the list
# for two counter, c1&c2 makes the intersection of their keys and the value is always 1
Leetcode with Python -> Sort的更多相关文章
- [LeetCode] 324. Wiggle Sort II 摆动排序 II
Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]... ...
- Python: sort,sorted,OrderedDict的用法
Python: sort,sorted,OrderedDict的用法 from http://stqdd.com/archives/427 by 莫亚菜 python对容器内数据的排序有两种,一种是容 ...
- LeetCode 15 3Sum [sort] <c++>
LeetCode 15 3Sum [sort] <c++> 给出一个一维数组,找出其中所有和为零的三元组(元素集相同的视作同一个三元组)的集合. C++ 先自己写了一发,虽然过了,但跑了3 ...
- LeetCode专题-Python实现之第28题: Implement strStr()
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第27题:Remove Element
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第26题:Remove Duplicates from Sorted Array
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第21题:Merge Two Sorted Lists
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第20题:Valid Parentheses
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第9题:Palindrome Number
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
随机推荐
- fluent Python
1.1 Python风格的纸牌 Python collections模块中的内置模块:namedtuple https://www.liaoxuefeng.com/wiki/0013747381250 ...
- Linux内存管理 - buddy系统
本文目的在于分析Linux内存管理机制中的伙伴系统.内核版本为2.6.31.1. 伙伴系统的概念 在系统运行过程中,经常需要分配一组连续的页,而频繁的申请和释放内存页会导致内存中散布着许多不连续的页, ...
- Servlet的工作原理和生命周期
Servlet的工作原理 . Web服务器加载Servlet:Web服务器启动后,它会根据每个工程的web.xml文件去查找该工程的Servlet,并且找到这些Servlet的Class文件所在的地址 ...
- 侯捷《C++面向对象开发》——动手实现自己的复数类
前言 最近在看侯捷的一套课程<C++面向对象开发>,刚看完第一节introduction之后就被疯狂圈粉.感觉侯捷所提及所重视的部分也正是我一知半解的知识盲区,我之前也写过一些C++面向对 ...
- yii2 的登录注册 轮子
//利用到了yii2 框架之中的验证规则 进行判定而已 也不是很高深的东西 但是 使用框架自身的轮子 会有安全性能的隐患 1注册reg controller 中 我都以admin 为例子 publi ...
- 01 mysql 基础一 (进阶)
mysql基础一 1.认识mysql与创建用户 01 Mysql简介 Mysql是最流行的关系型数据库管理系统之一,由瑞典MySQLAB公司开发,目前属于Oracle公司. MySQL是一种关联数据库 ...
- 进程、线程、协程和GIL(二)
上一篇博客讲了进程.线程.协程和GIL的基本概念,这篇我们来说说在以下三点: 1> python中使用threading库来创建线程的两种方式 2> 使用Event对消来判断线程是否已启动 ...
- POJ 3662 (二分+SPFA
Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8856 Accepted: 3211 D ...
- 26-dotnet watch run 和attach到进程调试
1-打开vscode, 按下Ctrl+`,打开命令行窗口 创建一个donet core mvc项目 2-打开刚刚创建的文件夹 3-输入 dotnet run 访问网站 4 -F5键即可调试 5-更改代 ...
- 笔记-HTTP及HTTPS
笔记-HTTP及HTTPS 1. HTTP 1.1. 简介 HTTP:hyper text transfer protocol 超文本传输协议 http常用请求方式,method ge ...