[LeetCode] 350. 两个数组的交集 II intersection-of-two-arrays-ii(排序)
2024-08-31 10:33:05
思路:
先找到set的交集,然后分别计算交集中的每个元素在两个原始数组中出现的最小次数。
class Solution(object):
def intersect(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: List[int]
"""
# intersect=[]
# for i in nums1:
# while i in nums2:
# intersect.append(i)
# return intersect #注释方法超出时间 temp=list(set(nums1)&set(nums2))
intersect=[]
for i in temp:
intersect+=[i]*min(nums1.count(i),nums2.count(i)) #list除了append,可以直接+ ;注意元素加[]
return intersect
但是这种方法还是属于调用函数,和要求的哈希表等基本无关。
最新文章
- [转]socket 通俗解释
- Spring-配置bean的方法(工厂方法和Factorybean)【转】
- ant打包webservice jar
- Majority Element II
- 使用 Google Analytics 跟踪 JavaScript 错误
- 修改weblogic部署的应用名称
- 【LeetCode 235】Lowest Common Ancestor of a Binary Search Tree
- 杭电ACM2091--空心三角形
- stick footer布局
- mysql数据库开启日志
- 关于IDE与环境变量的一点说明
- netty源码学习
- vue 相关
- datatable中的copy和clone的用法区分
- 请问浏览器访问www.baidu.com经历了怎样的过程?
- Scala语言笔记 - 第一篇
- 将Maven项目打包成可执行jar文件(引用第三方jar)
- MySQL技术内幕读书笔记(八)——事务
- Android的TextView设置padding无效
- Poj3176 Cow Bowling (动态规划 数字三角形)