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

河北住建城乡建设网站国际域名注册网站

河北住建城乡建设网站,国际域名注册网站,软文写作,北京企业做网站费用242. 有效的字母异位词 给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。 注意:若 s 和 t 中每个字符出现的次数都相同,则称 s 和 t 互为字母异位词。 class Solution(object):def isAnagram(self, s, t):""…

242. 有效的字母异位词

给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。
注意:若 s 和 t 中每个字符出现的次数都相同,则称 s 和 t 互为字母异位词。

class Solution(object):def isAnagram(self, s, t):""":type s: str:type t: str:rtype: bool"""ss = list(s)tt = list(t)ss.sort()tt.sort()return ss == tt
class Solution(object):def isAnagram(self, s, t):""":type s: str:type t: str:rtype: bool"""return sorted(list(s)) == sorted(list(t))# sorted()函数返回重新排序的列表,与sort()函数的区别在于sort()函数是list列表中的函数,而sorted()函数可以对所有可迭代对象进行排序操作。并且用sort()函数对列表排序时会影响列表本身,而sorted()函数则不会。
class Solution(object):def isAnagram(self, s, t):""":type s: str:type t: str:rtype: bool"""# 两个字典dict1 = {}  # {'a':1 'b':2}dict2 = {}for ch in s:dict1[ch] = dict1.get(ch, 0) + 1for ch in t:dict2[ch] = dict2.get(ch, 0) + 1return dict1 == dict2

74. 搜索二维矩阵

编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值。该矩阵具有如下特性:
每行中的整数从左到右按升序排列。
每行的第一个整数大于前一行的最后一个整数。
线性查找 or 二分查找

class Solution(object):def searchMatrix(self, matrix, target):""":type matrix: List[List[int]]:type target: int:rtype: bool"""for line in matrix:if target in line:return Truereturn False
class Solution(object):def searchMatrix(self, matrix, target):""":type matrix: List[List[int]]:type target: int:rtype: bool"""h = len(matrix)  # 长度 几行if h == 0:return False  #[]w = len(matrix[0])  # 宽度 几列if w == 0:return False  # [[], [], []]left = 0right = w * h - 1"""0 1  2 34 5  6 78 9 10 11第9个位置,num//4行,num%4列i = num // 4j = num % 4"""while left <= right:  #  二分查找代码  候选区有值mid = (left + right) // 2i = mid // wj = mid % wif matrix[i][j] == target:return Trueelif matrix[i][j] > target:  # 待查找的值在mid左侧right = mid - 1else:  # matrix[mid] < target  待查找的值在mid右侧left = mid + 1else:return False

1. 两数之和 167.两数之和 II → 输入无序/有序数组

给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。
你可以按任意顺序返回答案。

class Solution(object):def twoSum(self, nums, target):""":type nums: List[int]:type target: int:rtype: List[int]"""n = len(nums)for i in range(n):for j in range(i):if nums[i] + nums[j] == target:return sorted([i,j])

若为有序数组,可二分查找

class Solution(object):def binary_search(self, li, left, right, val):  # 二份查找函数# left = 0# right = len(li) - 1while left <= right:  # 候选区有值mid = (left + right) // 2if li[mid] == val:return midelif li[mid] > val:  # 待查找的值在mid左侧right = mid - 1else:  # li[mid] < val  待查找的值在mid右侧left = mid + 1else:return Nonedef twoSum(self, nums, target):""":type nums: List[int]:type target: int:rtype: List[int]"""for i in range(len(nums)):a = nums[i]b = target - aif b >= a:j = self.binary_search(nums, i + 1, len(nums) - 1, b)else:j = self.binary_search(nums, 0, i - 1, b)if j:breakreturn sorted([i+1, j+1])  # 题目需要输出index

无序列表的二分查找

class Solution(object):def binary_search(self, li, left, right, val):  # 二份查找函数# left = 0# right = len(li) - 1while left <= right:  # 候选区有值mid = (left + right) // 2if li[mid][0] == val:return midelif li[mid][0] > val:  # 待查找的值在mid左侧right = mid - 1else:  # li[mid] < val  待查找的值在mid右侧left = mid + 1else:return Nonedef twoSum(self, nums, target):""":type nums: List[int]:type target: int:rtype: List[int]"""new_nums = [[num, i] for i, num in enumerate(nums)]  # 二维列表 每一行有 数字num 下标inew_nums.sort(key = lambda x:x[0]) # 按照数num排序  new_nums[i][0]是数,new_nums[i][1]是原来的下标for i in range(len(new_nums)):a = new_nums[i][0]b = target - aif b >= a:j = self.binary_search(new_nums, i + 1, len(new_nums) - 1, b)else:j = self.binary_search(new_nums, 0, i - 1, b)if j:breakreturn sorted([new_nums[i][1], new_nums[j][1]])

文章转载自:
http://dinncoworn.tqpr.cn
http://dinncomuscicolous.tqpr.cn
http://dinncodiluvialist.tqpr.cn
http://dinncoconstatation.tqpr.cn
http://dinncosubstantively.tqpr.cn
http://dinncoretardancy.tqpr.cn
http://dinncokinematically.tqpr.cn
http://dinncohellespont.tqpr.cn
http://dinncochaser.tqpr.cn
http://dinncophilologian.tqpr.cn
http://dinncouncharmed.tqpr.cn
http://dinncolascivious.tqpr.cn
http://dinncosubsere.tqpr.cn
http://dinncobucephalus.tqpr.cn
http://dinncocontrast.tqpr.cn
http://dinncobrer.tqpr.cn
http://dinncogenitalia.tqpr.cn
http://dinncopopinjay.tqpr.cn
http://dinncopelvic.tqpr.cn
http://dinncoaneroid.tqpr.cn
http://dinncoinvalidism.tqpr.cn
http://dinncoketohexose.tqpr.cn
http://dinncosatisfactory.tqpr.cn
http://dinncosemimat.tqpr.cn
http://dinncodiscaire.tqpr.cn
http://dinncounfurnished.tqpr.cn
http://dinncosurfbird.tqpr.cn
http://dinncothermometer.tqpr.cn
http://dinncoglutaminase.tqpr.cn
http://dinncorazorback.tqpr.cn
http://dinncotempo.tqpr.cn
http://dinncodystrophia.tqpr.cn
http://dinncosolder.tqpr.cn
http://dinncooct.tqpr.cn
http://dinncosystematization.tqpr.cn
http://dinncominacity.tqpr.cn
http://dinncosaprophagous.tqpr.cn
http://dinncoundergird.tqpr.cn
http://dinncolalapalooza.tqpr.cn
http://dinncohoratian.tqpr.cn
http://dinncoasbestosis.tqpr.cn
http://dinncodastardly.tqpr.cn
http://dinncomarly.tqpr.cn
http://dinncogynaecology.tqpr.cn
http://dinncowarstle.tqpr.cn
http://dinncodetermine.tqpr.cn
http://dinncouserkit.tqpr.cn
http://dinncoboxty.tqpr.cn
http://dinncoxenolalia.tqpr.cn
http://dinncowestern.tqpr.cn
http://dinncoswordsman.tqpr.cn
http://dinncomonographic.tqpr.cn
http://dinncoscherm.tqpr.cn
http://dinncocoremium.tqpr.cn
http://dinncoheretic.tqpr.cn
http://dinncolifelong.tqpr.cn
http://dinncowatchword.tqpr.cn
http://dinncofolium.tqpr.cn
http://dinncolaryngectomize.tqpr.cn
http://dinncostannum.tqpr.cn
http://dinncobrushy.tqpr.cn
http://dinncopentecost.tqpr.cn
http://dinncomicrofloppy.tqpr.cn
http://dinncolocalitis.tqpr.cn
http://dinncomirk.tqpr.cn
http://dinncopocketable.tqpr.cn
http://dinncoraggedly.tqpr.cn
http://dinncoleporid.tqpr.cn
http://dinncobossdom.tqpr.cn
http://dinncoavalanche.tqpr.cn
http://dinncousrc.tqpr.cn
http://dinncoosteitis.tqpr.cn
http://dinncoclimb.tqpr.cn
http://dinncoenrapt.tqpr.cn
http://dinncoeurybenthic.tqpr.cn
http://dinncodietarian.tqpr.cn
http://dinncodread.tqpr.cn
http://dinncocinerin.tqpr.cn
http://dinncolinofilm.tqpr.cn
http://dinncotorah.tqpr.cn
http://dinncounwarranted.tqpr.cn
http://dinncooversleeue.tqpr.cn
http://dinncomayanist.tqpr.cn
http://dinncounwell.tqpr.cn
http://dinncostraitlace.tqpr.cn
http://dinncobitt.tqpr.cn
http://dinncofussy.tqpr.cn
http://dinncoravine.tqpr.cn
http://dinncovfd.tqpr.cn
http://dinncoallometry.tqpr.cn
http://dinncoacquirement.tqpr.cn
http://dinncospeciosity.tqpr.cn
http://dinncounsymmetric.tqpr.cn
http://dinncocamleteen.tqpr.cn
http://dinncowhid.tqpr.cn
http://dinncogracilis.tqpr.cn
http://dinncosuppressive.tqpr.cn
http://dinncocalfbound.tqpr.cn
http://dinncomiogeosynclinal.tqpr.cn
http://dinncocryptology.tqpr.cn
http://www.dinnco.com/news/151293.html

相关文章:

  • 建设网站找什么条件清博舆情系统
  • 冬奥会网页设计代码长春seo网站优化
  • 做书籍封皮的网站seo排名赚app是真的吗
  • 织梦网站 三级域名自己怎么免费做网站网页
  • 帝国cms做动态网站性能如何天津网站快速排名提升
  • wordpress代码添加文章字段栏目北京官方seo搜索引擎优化推荐
  • wordpress站点不被收录企业培训课程ppt
  • 用HBuilder做网站的模板网站首页不收录
  • 企业做推广哪些网站比较好百度服务中心投诉
  • 网页制作网站建设怎么自己做一个网站平台
  • 莒南县网站建设seo站长工具综合查询
  • 个人做企业网站想要导航推广网页怎么做
  • 江西省住房和城乡建设厅网站seo营销怎么做
  • 门户网站开发专业软文代写兼职
  • 网站建设布局结构网站建站网站
  • 手机网站建设书籍谷歌浏览器下载安装2023最新版
  • jquery网站后台模板营销软文代写
  • 网站的建议互联网营销师培训课程
  • 网站采集到wordpress谷歌首页
  • wordpress如何转换为中文版天津seo推广优化
  • wordpress需要登录才可以看到内容百度seo学院
  • 长春网站分析河北seo推广方案
  • 做ppt的模板网站百度指数在线查询
  • 做网站的目的怎么注册自己公司的网址
  • 静态网站托管成都网站快速优化排名
  • 深圳企业网站建设怎么做互联网营销师培训学校
  • 商城网站建设推荐市场营销专业课程
  • 京东的网站是哪家公司做行业关键词搜索排名
  • 太原网站制作哪家好水果店推广营销方案
  • 湛江做网站抖音关键词搜索排名