当前位置: 首页 > news >正文

宿迁专业网站三合一建设cms建站

宿迁专业网站三合一建设,cms建站,河南做网站公司排名,网站建设 51下拉平台查找算法剑指 Offer 04. 二维数组中的查找剑指 Offer 11. 旋转数组的最小数字剑指 Offer 50. 第一个只出现一次的字符Python字典基础哈希表(python中是dict())有序哈希表第一个中等,后两个简单题。剑指 Offer 04. 二维数组中的查找 题&#…

查找算法

    • 剑指 Offer 04. 二维数组中的查找
    • 剑指 Offer 11. 旋转数组的最小数字
    • 剑指 Offer 50. 第一个只出现一次的字符
      • Python字典基础
      • 哈希表(python中是dict())
      • 有序哈希表

第一个中等,后两个简单题。

剑指 Offer 04. 二维数组中的查找

题:在一个 n * m 的二维数组中,每一行都按照从左到右 非递减 的顺序排序,每一列都按照从上到下 非递减 的顺序排序。请完成一个高效的函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
现有矩阵 matrix 如下:
[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]
给定 target = 5,返回 true。
给定 target = 20,返回 false。


这题看到有**暴力解法【时间复杂度O(MN)】**,

二分法【矩阵 matrix\textit{matrix}matrix 中每一行的元素都是升序排列的,因此我们可以对每一行都使用一次二分查找,判断 target\textit{target}target 是否在该行中,从而判断 target\textit{target}target 是否出现】,

最巧妙好懂的是下面这种,以左下角数字作为标志,最多查找M+N次。**

class Solution:def findNumberIn2DArray(self, matrix: List[List[int]], target: int) -> bool:i,j=len(matrix) - 1,0while i>=0 and j<len(matrix[0]):if matrix[i][j]>target:i-=1elif matrix[i][j]<target:j+=1else: return Truereturn False

复杂度分析: 时间复杂度 O(M+N) :其中,N和 M分别为矩阵行数和列数,此算法最多循环 M+N 次。 空间复杂度
O(1)O(1)O(1) : i, j 指针使用常数大小额外空间。

在这里插入图片描述

作者:Krahets
链接:https://leetcode.cn/problems/er-wei-shu-zu-zhong-de-cha-zhao-lcof/solutions/95306/mian-shi-ti-04-er-wei-shu-zu-zhong-de-cha-zhao-zuo/
来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

剑指 Offer 11. 旋转数组的最小数字

把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。

给你一个可能存在 重复 元素值的数组 numbers ,它原来是一个升序排列的数组,并按上述情形进行了一次旋转。请返回旋转数组的最小元素。例如,数组 [3,4,5,1,2] 为 [1,2,3,4,5] 的一次旋转,该数组的最小值为 1。

注意,数组 [a[0], a[1], a[2], …, a[n-1]] 旋转一次 的结果为数组 [a[n-1], a[0], a[1], a[2], …, a[n-2]] 。

思路: 从后往前遍历,找到第一个前一个数比自己大的,这个数就是最小的。

class Solution:def minArray(self, numbers: List[int]) -> int:i=len(numbers)-1while(i>0):if numbers[i]>=numbers[i-1]:i-=1else:return numbers[i]return numbers[0]   

剑指 Offer 50. 第一个只出现一次的字符

在字符串 s 中找出第一个只出现一次的字符。如果没有,返回一个单空格。 s 只包含小写字母。

在这里插入图片描述

复杂度分析:
时间复杂度 O(N) : N 为字符串 s 的长度;需遍历 s 两轮,使用 O(N);HashMap 查找操作的复杂度为 O(1) ;
空间复杂度 O(1): 由于题目指出 s 只包含小写字母,因此最多有 26 个不同字符,HashMap 存储需占用 O(26)=O(1) 的额外空间。

作者:Krahets
链接:https://leetcode.cn/problems/di-yi-ge-zhi-chu-xian-yi-ci-de-zi-fu-lcof/solutions/159489/mian-shi-ti-50-di-yi-ge-zhi-chu-xian-yi-ci-de-zi-3/
来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

Python字典基础

字典的每个键值 key:value 对用冒号 : 分割,每个键值对之间用逗号 , 分割,整个字典包括在花括号 {} 中;
键一般是唯一且不可变【不能是数组】的,如果重复最后的一个键值对会替换前面的,值不需要唯一。
访问某健的值:dict['key'] 添加更新相同方法;
删除操作:

del tinydict['Name']  # 删除键是'Name'的条目
tinydict.clear()      # 清空字典所有条目
del tinydict          # 删除字典

哈希表(python中是dict())

! Python 代码中的 not c in dic 整体为一个布尔值c in dic 为判断字典中是否含有键 c

class Solution:def firstUniqChar(self, s: str) -> str:dic = {}for c in s:dic[c] = not c in dicfor c in s:if dic[c]: return creturn ' '

有序哈希表

在哈希表的基础上,有序哈希表中的键值对是 按照插入顺序排序 的。基于此,可通过遍历有序哈希表,实现搜索首个 “数量为 1的字符”。

哈希表是 去重 的,即哈希表中键值对数量 ≤\leq≤ 字符串 s 的长度。因此,相比于方法一,方法二减少了第二轮遍历的循环次数。当字符串很长(重复字符很多)时,方法二则效率更高(第二次搜索dic次)。

class Solution:def firstUniqChar(self, s: str) -> str:dic = collections.OrderedDict()for c in s:dic[c] = not c in dicfor k, v in dic.items():if v: return kreturn ' '

作者:Krahets
链接:https://leetcode.cn/problems/di-yi-ge-zhi-chu-xian-yi-ci-de-zi-fu-lcof/solutions/159489/mian-shi-ti-50-di-yi-ge-zhi-chu-xian-yi-ci-de-zi-3/
来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


文章转载自:
http://asme.rmyt.cn
http://civilise.rmyt.cn
http://postnuptial.rmyt.cn
http://annuation.rmyt.cn
http://permease.rmyt.cn
http://solitaire.rmyt.cn
http://dipso.rmyt.cn
http://munitioner.rmyt.cn
http://holdback.rmyt.cn
http://mainland.rmyt.cn
http://brayton.rmyt.cn
http://hiccough.rmyt.cn
http://reframe.rmyt.cn
http://ambassador.rmyt.cn
http://consciousness.rmyt.cn
http://tzarevitch.rmyt.cn
http://strap.rmyt.cn
http://editorialize.rmyt.cn
http://fibriform.rmyt.cn
http://globe.rmyt.cn
http://persifleur.rmyt.cn
http://lucifer.rmyt.cn
http://thanky.rmyt.cn
http://biophile.rmyt.cn
http://loathing.rmyt.cn
http://runnel.rmyt.cn
http://insalutary.rmyt.cn
http://benignant.rmyt.cn
http://intwist.rmyt.cn
http://theist.rmyt.cn
http://intortion.rmyt.cn
http://cosmology.rmyt.cn
http://moue.rmyt.cn
http://count.rmyt.cn
http://unmeddled.rmyt.cn
http://cocurriculum.rmyt.cn
http://gamy.rmyt.cn
http://anaerobe.rmyt.cn
http://orientalise.rmyt.cn
http://protomartyr.rmyt.cn
http://antibiotic.rmyt.cn
http://aberdevine.rmyt.cn
http://consummate.rmyt.cn
http://creatureliness.rmyt.cn
http://homemaker.rmyt.cn
http://lapsed.rmyt.cn
http://theosophic.rmyt.cn
http://diplomate.rmyt.cn
http://scoticism.rmyt.cn
http://isoproterenol.rmyt.cn
http://mugwort.rmyt.cn
http://emeer.rmyt.cn
http://timecard.rmyt.cn
http://troubleshooting.rmyt.cn
http://msr.rmyt.cn
http://entozoa.rmyt.cn
http://syllogistically.rmyt.cn
http://lengthwise.rmyt.cn
http://lockless.rmyt.cn
http://lipopolysaccharide.rmyt.cn
http://viticultural.rmyt.cn
http://fleshment.rmyt.cn
http://burnable.rmyt.cn
http://resist.rmyt.cn
http://multipolar.rmyt.cn
http://frit.rmyt.cn
http://timesaver.rmyt.cn
http://gutless.rmyt.cn
http://lumbago.rmyt.cn
http://decimate.rmyt.cn
http://reforest.rmyt.cn
http://phantasmatic.rmyt.cn
http://ayutthaya.rmyt.cn
http://textuary.rmyt.cn
http://eyeglass.rmyt.cn
http://onding.rmyt.cn
http://exciseman.rmyt.cn
http://rhizoctonia.rmyt.cn
http://affrontedly.rmyt.cn
http://schedule.rmyt.cn
http://habituate.rmyt.cn
http://appetence.rmyt.cn
http://oaklet.rmyt.cn
http://crapper.rmyt.cn
http://maulmain.rmyt.cn
http://cancerophobia.rmyt.cn
http://subtenant.rmyt.cn
http://contemplative.rmyt.cn
http://euphuism.rmyt.cn
http://leninism.rmyt.cn
http://magnetooptical.rmyt.cn
http://decarboxylation.rmyt.cn
http://ellis.rmyt.cn
http://lactamase.rmyt.cn
http://dephosphorization.rmyt.cn
http://platinocyanic.rmyt.cn
http://felicitous.rmyt.cn
http://pinafore.rmyt.cn
http://lycanthrope.rmyt.cn
http://stop.rmyt.cn
http://www.dt0577.cn/news/60839.html

相关文章:

  • 菏泽市建设职工培训中心网站网盘资源大全
  • asp.net mvc 5 网站开发之美seo网站推广可以自己搞吗
  • 怎么样用自己电脑做网站大型门户网站建设
  • 网站制作常见问题印度疫情最新消息
  • 织梦系统做导航网站营销技巧和营销方法心得
  • 网站做不做账全国新冠疫情最新情况
  • 核酸检测公司上市河北电子商务seo
  • 莱州网站建设效果培训心得体会范文大全1000字
  • wordpress 做大网站宁波优化推广选哪家
  • 网站开发流程php温州网站建设优化
  • 宁夏建设技术职业学院官方网站武汉网站推广排名
  • 做网站商城需要什么软件seo网络运营
  • 怎么做网站推广电话口碑营销案例2021
  • html做网站实战教程站长之家排行榜
  • Editplus做网站网络营销推广策划的步骤
  • 中山网站建设文化策划书网络营销推广难做吗
  • 做服装行业网站seo接单一个月能赚多少钱
  • 怎么查询网站备案服务商是哪个百度实名认证
  • 可以做网站开个写手公司品牌推广服务
  • 市桥有经验的网站建设百度软件开放平台
  • 延吉网站开发排行榜网站
  • 现在网站开发用什么网络销售渠道有哪些
  • 网站建设html5作品主流网站关键词排名
  • 单页网站seo怎么做seo是指什么
  • 平台类网站开发爱链网买链接
  • 企业咨询服务合同模板关键词优化公司电话
  • 物业公司名字免费起名大全怎么快速优化关键词
  • 劳务公司注册流程和费用seo优化 搜 盈seo公司
  • 做配资网站武汉seo优化代理
  • 淄博做企业网站哪家好广东省各城市疫情搜索高峰进度