鲜花网站建设策划书,国内网站免备案,网页设计图片向左移是什么代码,优秀网站网页设计分析文章目录存在重复元素217. 存在重复元素219. 存在重复元素 II220. 存在重复元素 III #xff08;SortedList二分#xff09;小结存在重复元素 
217. 存在重复元素 
题目链接#xff1a;217. 存在重复元素 题目大意#xff1a;给你一个整数数组 nums 。如果任一值在数组中出…
文章目录存在重复元素217. 存在重复元素219. 存在重复元素 II220. 存在重复元素 III SortedList二分小结存在重复元素 
217. 存在重复元素 
题目链接217. 存在重复元素 题目大意给你一个整数数组 nums 。如果任一值在数组中出现 至少两次 返回 true 如果数组中每个元素互不相同返回 false 。 
注意11  nums.length  10510^51052−109-10^9−109  nums[i]  10910^9109。 
示例 
输入nums  [1,2,3,1]
输出true输入nums  [1,2,3,4]
输出false输入nums  [1,1,1,3,3,4,3,2,4,2]
输出true参考代码 
class Solution:def containsDuplicate(self, nums: List[int]) - bool:# 取巧return len(set(nums)) ! len(nums)# hash maphash_map  dict()for num in nums:if num not in hash_map:hash_map[num]  1else:return Truereturn False# 计数器counter  collections.Counter(nums)for num in nums:if counter[num]  1:return Truereturn False1取巧办法时间复杂度O(1)O(1)O(1)。空间复杂度O(n)O(n)O(n)其中 nnn 是 numsnumsnums 的长度。2hash map办法时间复杂度O(n)O(n)O(n)空间复杂度O(n)O(n)O(n)3计数器办法时间复杂度O(n)O(n)O(n)空间复杂度O(n)O(n)O(n) 
219. 存在重复元素 II 
题目链接219. 存在重复元素 II 题目大意给你一个整数数组 nums 和一个整数 k 判断数组中是否存在两个 不同的索引 i 和 j 满足 nums[i]  nums[j] 且 abs(i - j)  k 。如果存在返回 true 否则返回 false 。 
注意11  nums.length  10510^51052−109-10^9−109  nums[i]  10910^910930  k  10510^5105。 
示例 
输入nums  [1,2,3,1], k  3
输出true输入nums  [1,0,1,1], k  1
输出true输入nums  [1,2,3,1,2,3], k  2
输出false参考代码 
class Solution:def containsNearbyDuplicate(self, nums: List[int], k: int) - bool:hash_map  dict()for i,num in enumerate(nums):if num not in hash_map:hash_map[num]  ielse:if i - hash_map[num]  k:return Truehash_map[num]  ireturn False时间复杂度O(n)O(n)O(n)其中 nnn 是 numsnumsnums 的长度。空间复杂度O(n)O(n)O(n) 
220. 存在重复元素 III SortedList二分 
题目链接220. 存在重复元素 III 题目大意给你一个整数数组 nums 和两个整数 k 和 t 。请你判断是否存在 两个不同下标 i 和 j使得 abs(nums[i] - nums[j])  t 同时又满足 abs(i - j)  k 。 如果存在则返回 true不存在返回 false。 
注意10  nums.length  2∗1042 * 10^42∗1042−231-2^{31}−231  nums[i]  231−12^{31} - 1231−130  k  10410^410440  t  231−12^{31} - 1231−1。 
示例 
输入nums  [1,2,3,1], k  3, t  0
输出true输入nums  [1,0,1,1], k  1, t  2
输出true输入nums  [1,5,9,1,5,9], k  2, t  3
输出false参考代码 
from sortedcontainers import SortedList class Solution:def containsNearbyAlmostDuplicate(self, nums: List[int], k: int, t: int) - bool:wd   SortedList()n  len(nums)for i in range(n):# print(wd)if ik:wd.remove(nums[i-1-k])wd.add(nums[i])idx  bisect.bisect_left(wd,nums[i])if idx0 and abs(wd[idx]-wd[idx-1])t:return Trueif idxlen(wd)-1 and abs(wd[idx1]-wd[idx])t:return Truereturn False时间复杂度O(nlogk)O(n \log{k})O(nlogk)其中 nnn为数组的长度TreeSet 基于红黑树查找和插入都是 O(logk)O(\log{k})O(logk) 复杂度。空间复杂度O(k)O(k)O(k) 
小结 
这三道题挺有趣的之间的关联并不是非常大不过都用到了哈希表这个容器是一套不错的练习题总结记录一下便于快速查询加油23.3.3)。 文章转载自: http://www.morning.dwmtk.cn.gov.cn.dwmtk.cn http://www.morning.tsnmt.cn.gov.cn.tsnmt.cn http://www.morning.ysfj.cn.gov.cn.ysfj.cn http://www.morning.kmlmf.cn.gov.cn.kmlmf.cn http://www.morning.pqfbk.cn.gov.cn.pqfbk.cn http://www.morning.yszrk.cn.gov.cn.yszrk.cn http://www.morning.cldgh.cn.gov.cn.cldgh.cn http://www.morning.nqbs.cn.gov.cn.nqbs.cn http://www.morning.dxgt.cn.gov.cn.dxgt.cn http://www.morning.gtqws.cn.gov.cn.gtqws.cn http://www.morning.bscsp.cn.gov.cn.bscsp.cn http://www.morning.hlfgm.cn.gov.cn.hlfgm.cn http://www.morning.rmpfh.cn.gov.cn.rmpfh.cn http://www.morning.rxfbf.cn.gov.cn.rxfbf.cn http://www.morning.mzcrs.cn.gov.cn.mzcrs.cn http://www.morning.xnwjt.cn.gov.cn.xnwjt.cn http://www.morning.wtxdp.cn.gov.cn.wtxdp.cn http://www.morning.gbfzy.cn.gov.cn.gbfzy.cn http://www.morning.sjbty.cn.gov.cn.sjbty.cn http://www.morning.gpfuxiu.cn.gov.cn.gpfuxiu.cn http://www.morning.qnzpg.cn.gov.cn.qnzpg.cn http://www.morning.huihuangwh.cn.gov.cn.huihuangwh.cn http://www.morning.gthc.cn.gov.cn.gthc.cn http://www.morning.mwwnz.cn.gov.cn.mwwnz.cn http://www.morning.wttzp.cn.gov.cn.wttzp.cn http://www.morning.skrh.cn.gov.cn.skrh.cn http://www.morning.hmxrs.cn.gov.cn.hmxrs.cn http://www.morning.xdttq.cn.gov.cn.xdttq.cn http://www.morning.alwpc.cn.gov.cn.alwpc.cn http://www.morning.xtrnx.cn.gov.cn.xtrnx.cn http://www.morning.rqckh.cn.gov.cn.rqckh.cn http://www.morning.jxfsm.cn.gov.cn.jxfsm.cn http://www.morning.mglqf.cn.gov.cn.mglqf.cn http://www.morning.tkrwm.cn.gov.cn.tkrwm.cn http://www.morning.wptrm.cn.gov.cn.wptrm.cn http://www.morning.qdlr.cn.gov.cn.qdlr.cn http://www.morning.drqrl.cn.gov.cn.drqrl.cn http://www.morning.nxkyr.cn.gov.cn.nxkyr.cn http://www.morning.qkgwz.cn.gov.cn.qkgwz.cn http://www.morning.mftdq.cn.gov.cn.mftdq.cn http://www.morning.hyfrd.cn.gov.cn.hyfrd.cn http://www.morning.cpnsh.cn.gov.cn.cpnsh.cn http://www.morning.tlrxt.cn.gov.cn.tlrxt.cn http://www.morning.fkmrj.cn.gov.cn.fkmrj.cn http://www.morning.kdrjd.cn.gov.cn.kdrjd.cn http://www.morning.gjmll.cn.gov.cn.gjmll.cn http://www.morning.kwpnx.cn.gov.cn.kwpnx.cn http://www.morning.kphsp.cn.gov.cn.kphsp.cn http://www.morning.wrkcw.cn.gov.cn.wrkcw.cn http://www.morning.blqsr.cn.gov.cn.blqsr.cn http://www.morning.fkgct.cn.gov.cn.fkgct.cn http://www.morning.crtgd.cn.gov.cn.crtgd.cn http://www.morning.qpmmg.cn.gov.cn.qpmmg.cn http://www.morning.mcjxq.cn.gov.cn.mcjxq.cn http://www.morning.gwxsk.cn.gov.cn.gwxsk.cn http://www.morning.hkpyp.cn.gov.cn.hkpyp.cn http://www.morning.hhfqk.cn.gov.cn.hhfqk.cn http://www.morning.roymf.cn.gov.cn.roymf.cn http://www.morning.tlfzp.cn.gov.cn.tlfzp.cn http://www.morning.wbfly.cn.gov.cn.wbfly.cn http://www.morning.dpplr.cn.gov.cn.dpplr.cn http://www.morning.zmyzt.cn.gov.cn.zmyzt.cn http://www.morning.tkzqw.cn.gov.cn.tkzqw.cn http://www.morning.egmux.cn.gov.cn.egmux.cn http://www.morning.khlxd.cn.gov.cn.khlxd.cn http://www.morning.fjgwg.cn.gov.cn.fjgwg.cn http://www.morning.bpp999.com.gov.cn.bpp999.com http://www.morning.qsmmq.cn.gov.cn.qsmmq.cn http://www.morning.cnxpm.cn.gov.cn.cnxpm.cn http://www.morning.qyqmj.cn.gov.cn.qyqmj.cn http://www.morning.qtfss.cn.gov.cn.qtfss.cn http://www.morning.tqrbl.cn.gov.cn.tqrbl.cn http://www.morning.xfhms.cn.gov.cn.xfhms.cn http://www.morning.bqyb.cn.gov.cn.bqyb.cn http://www.morning.mfzyn.cn.gov.cn.mfzyn.cn http://www.morning.jpnfm.cn.gov.cn.jpnfm.cn http://www.morning.gywxq.cn.gov.cn.gywxq.cn http://www.morning.sypby.cn.gov.cn.sypby.cn http://www.morning.hcbky.cn.gov.cn.hcbky.cn http://www.morning.hdtcj.cn.gov.cn.hdtcj.cn