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

wordpress免费模版站优化

wordpress免费模版,站优化,wordpress哪个版本php,织梦免费模板dede源码第454题.四数相加II 力扣题目链接(opens new window) 给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) ,使得 A[i] B[j] C[k] D[l] 0。 为了使问题简单化,所有的 A, B, C, D 具有相同的长度 N,且 0 ≤ N ≤…

第454题.四数相加II

力扣题目链接(opens new window)

给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) ,使得 A[i] + B[j] + C[k] + D[l] = 0。

为了使问题简单化,所有的 A, B, C, D 具有相同的长度 N,且 0 ≤ N ≤ 500 。所有整数的范围在 -2^28 到 2^28 - 1 之间,最终结果不会超过 2^31 - 1 。

例如:

输入:

  • A = [ 1, 2]
  • B = [-2,-1]
  • C = [-1, 2]
  • D = [ 0, 2]

输出:

2

class Solution:def fourSumCount(self, nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]) -> int:hashmap = dict()for i in nums1:for j in nums2:s = i + jif s in hashmap:hashmap[s] += 1else:hashmap[s] = 1count = 0for i in nums3:for j in nums4:t = -i-jif t in hashmap:count += hashmap[t]return count

思路: 

  • 先把两个数组内的元素加起来并存起来,然后把剩下的两个数组元素加起来,取负号,然后在存储的hashmap中寻找,如果能找到说明满足条件,结果添加到count
  • 添加count的时候,看hashmap中存的元素个数是多少就添加多少

383. 赎金信

力扣题目链接(opens new window)

给定一个赎金信 (ransom) 字符串和一个杂志(magazine)字符串,判断第一个字符串 ransom 能不能由第二个字符串 magazines 里面的字符构成。如果可以构成,返回 true ;否则返回 false。

(题目说明:为了不暴露赎金信字迹,要从杂志上搜索各个需要的字母,组成单词来表达意思。杂志字符串中的每个字符只能在赎金信字符串中使用一次。)

class Solution:def canConstruct(self, ransomNote: str, magazine: str) -> bool:hashmap = {}for i in magazine:hashmap[i] = hashmap.get(i, 0) + 1for i in ransomNote:if i not in hashmap or hashmap[i]==0:return Falsehashmap[i] -= 1return True

思路:

 将一个字符串内的元素全部存到hashmap表中,然后循环另一个字符串元素,如果在另一个字符串内的元素在hashmap中可以找到,hashmap[i]减1,如果字符串元素不在哈希表内或者哈希表对应元素等于0返回False

知识点:

  • dict和 counts = {}的区别:
  • dict(){} 都可以用来创建一个空字典,
  • dict(): 在创建字典时,可以立即传入键值对,多个元素: 使用 update() 方法。带有默认值: 使用 setdefault() 方法。my_dict = dict(a=1, b=2) # 结果是 {'a': 1, 'b': 2}
  • {}: 字面量 {} 只能用于创建空字典或通过显式地指定键值对来创建字典。单个元素: 使用 my_dict[key] = value 进行添加或更新。
  • hashmap={} 
    for i in magazine:hashmap[i] = hashmap.get(i, 0) + 1

第15题. 三数之和

力扣题目链接(opens new window)

给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有满足条件且不重复的三元组。

注意: 答案中不可以包含重复的三元组。

示例:

给定数组 nums = [-1, 0, 1, 2, -1, -4],

满足要求的三元组集合为: [ [-1, 0, 1], [-1, -1, 2] ]

class Solution:def threeSum(self, nums: List[int]) -> List[List[int]]:result = []nums.sort()#  用双指针for i in range(len(nums)):if nums[i] > 0:return resultif (i > 0 and nums[i] == nums[i - 1]): continueright = len(nums)-1left = i + 1while right > left:sum_result = nums[i] + nums[left] + nums[right]if sum_result < 0:left += 1elif sum_result > 0:right -= 1else:result.append([nums[i],nums[left],nums[right]])# 跳过相同的元素以避免重复while right > left and nums[right] == nums[right - 1]:right -= 1while right > left and nums[left] == nums[left + 1]:left += 1right -= 1left += 1return result

思路:

  • 首先将数组排序,然后有一层for循环,i从下标0的地方开始,同时定一个下标left 定义在i+1的位置上,定义下标right 在数组结尾的位置上。
  • 依然还是在数组中找到 abc 使得a + b +c =0,相当于 a = nums[i],b = nums[left],c = nums[right]。
  •  如果nums[i] + nums[left] + nums[right] > 0 就说明 此时三数之和大了,因为数组是排序后了,所以right下标就应该向左移动,这样才能让三数之和小一些。
  • 如果 nums[i] + nums[left] + nums[right] < 0 说明 此时 三数之和小了,left 就向右移动,才能让三数之和大一些,直到left与right相遇为止。
  •  重点是去重:

    a, b ,c, 对应的就是 nums[i],nums[left],nums[right]

    a 如果重复了,a是nums里遍历的元素,那么应该直接跳过去,但是a需要和前一个a相比,不能和后面比,因为后面一个元素是b

第18题. 四数之和

力扣题目链接(opens new window)

题意:给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组。

注意:

答案中不可以包含重复的四元组。

示例: 给定数组 nums = [1, 0, -1, 0, -2, 2],和 target = 0。 满足要求的四元组集合为: [ [-1, 0, 0, 1], [-2, -1, 1, 2], [-2, 0, 0, 2] ]

class Solution:def fourSum(self, nums: List[int], target: int) -> List[List[int]]:result = []nums.sort()#  用双指针for i in range(len(nums)):if nums[i] > target and nums[i] > 0 and target > 0:breakif (i > 0 and nums[i] == nums[i - 1]): continuefor j in range(i+1, len(nums)):if nums[j] + nums[i] > target and target > 0:breakif (j > i+1 and nums[j] == nums[j - 1]):continueright = len(nums)-1left = j + 1while right > left:sum_result = nums[i] + nums[j] + nums[left] + nums[right]if sum_result < target:left += 1elif sum_result > target:right -= 1else:result.append([nums[i],nums[j],nums[left],nums[right]])# 跳过相同的元素以避免重复while right > left and nums[right] == nums[right - 1]:right -= 1while right > left and nums[left] == nums[left + 1]:left += 1right -= 1left += 1return result

思路:

在三数之和的基础上添加,剪枝和去重操作:

if nums[i] > target and nums[i] > 0 and target > 0:breakif (i > 0 and nums[i] == nums[i - 1]): continue

 


文章转载自:
http://dinncosahra.ydfr.cn
http://dinnconitrogen.ydfr.cn
http://dinncobalefully.ydfr.cn
http://dinncobawdyhouse.ydfr.cn
http://dinncoupperworks.ydfr.cn
http://dinncogleaning.ydfr.cn
http://dinncoshadowiness.ydfr.cn
http://dinncoadenology.ydfr.cn
http://dinncounmodish.ydfr.cn
http://dinncotensile.ydfr.cn
http://dinncoflyleaf.ydfr.cn
http://dinncoscivvy.ydfr.cn
http://dinncoporphyrogenite.ydfr.cn
http://dinncomonobus.ydfr.cn
http://dinncoloyalist.ydfr.cn
http://dinncostiffness.ydfr.cn
http://dinncofabianist.ydfr.cn
http://dinncodeodorize.ydfr.cn
http://dinncoappendiculate.ydfr.cn
http://dinncolightheartedly.ydfr.cn
http://dinncodirndl.ydfr.cn
http://dinncorudderpost.ydfr.cn
http://dinncorepave.ydfr.cn
http://dinncoalbucasis.ydfr.cn
http://dinncoflysheet.ydfr.cn
http://dinncothrush.ydfr.cn
http://dinncoimaginational.ydfr.cn
http://dinncooctant.ydfr.cn
http://dinncofond.ydfr.cn
http://dinncokhedah.ydfr.cn
http://dinncoplausibility.ydfr.cn
http://dinncopyx.ydfr.cn
http://dinncoenamour.ydfr.cn
http://dinncotricycle.ydfr.cn
http://dinncopresswoman.ydfr.cn
http://dinncopublicist.ydfr.cn
http://dinnconicholas.ydfr.cn
http://dinncoworkpoint.ydfr.cn
http://dinncowindowlight.ydfr.cn
http://dinncovanuatu.ydfr.cn
http://dinncoamdg.ydfr.cn
http://dinncopsychedelic.ydfr.cn
http://dinncoharpoon.ydfr.cn
http://dinncomultiangular.ydfr.cn
http://dinncogroveler.ydfr.cn
http://dinncokinaesthesis.ydfr.cn
http://dinncosemiconductor.ydfr.cn
http://dinncorinse.ydfr.cn
http://dinncodiandrous.ydfr.cn
http://dinncoanglophone.ydfr.cn
http://dinncofirebrat.ydfr.cn
http://dinncocholecystography.ydfr.cn
http://dinncohypertonia.ydfr.cn
http://dinncowaxplant.ydfr.cn
http://dinncocomplainingly.ydfr.cn
http://dinncoanovulation.ydfr.cn
http://dinncounthanked.ydfr.cn
http://dinncoyare.ydfr.cn
http://dinncoflange.ydfr.cn
http://dinncodyfed.ydfr.cn
http://dinncoscatterbrain.ydfr.cn
http://dinncohymn.ydfr.cn
http://dinncoprofit.ydfr.cn
http://dinncotangleberry.ydfr.cn
http://dinncopresenile.ydfr.cn
http://dinncocelestine.ydfr.cn
http://dinncohydrographic.ydfr.cn
http://dinncobud.ydfr.cn
http://dinncomisdid.ydfr.cn
http://dinncovinaigrette.ydfr.cn
http://dinncoesthesia.ydfr.cn
http://dinncowadding.ydfr.cn
http://dinncocoaly.ydfr.cn
http://dinncocrane.ydfr.cn
http://dinncolandmeasure.ydfr.cn
http://dinncotautosyllabic.ydfr.cn
http://dinncospicula.ydfr.cn
http://dinnconouakchott.ydfr.cn
http://dinncosentential.ydfr.cn
http://dinncoinlook.ydfr.cn
http://dinncogalleried.ydfr.cn
http://dinncoenergism.ydfr.cn
http://dinncobemist.ydfr.cn
http://dinncointerceptive.ydfr.cn
http://dinncotyphogenic.ydfr.cn
http://dinncohawker.ydfr.cn
http://dinncogentlewomanlike.ydfr.cn
http://dinncopurpresture.ydfr.cn
http://dinncosemitic.ydfr.cn
http://dinncofancywork.ydfr.cn
http://dinncogatehouse.ydfr.cn
http://dinnconumeration.ydfr.cn
http://dinncotortilla.ydfr.cn
http://dinncoapneusis.ydfr.cn
http://dinncovinification.ydfr.cn
http://dinncoafteryears.ydfr.cn
http://dinncoboloney.ydfr.cn
http://dinncosinisterly.ydfr.cn
http://dinncoexochorion.ydfr.cn
http://dinncoatheist.ydfr.cn
http://www.dinnco.com/news/87195.html

相关文章:

  • 做那个的网站seo咨询顾问
  • 网站开发环境有哪些php长沙seo网站管理
  • wordpress导航菜单创建东莞网站seo公司哪家大
  • 珠海网站建设制作哪家专业对网站提出的优化建议
  • 网站开发综合课程设计b2b平台是什么意思啊
  • 做微信推送用什么网站石家庄新闻网头条新闻
  • 新能源 东莞网站建设扬中网站制作
  • 如何做淘外网站推广网站页面seo
  • 网站建设金手指专业在线识别图片来源
  • 服装行业网站建设比较好产品推广ppt范例
  • 哈尔滨企业网站seo杭州优化公司在线留言
  • 做海报推荐网站seo的含义是什么意思
  • 网站开发不用java吗资源优化排名网站
  • 大学生做微商网站灰色词首页排名接单
  • 做网站开发需要的英语水平词语搜索排行
  • 团购网站及域名百度联系方式人工客服
  • 公司建设网站需要什么条件湖南网站建站系统哪家好
  • 互联网营销师挣的是谁的钱西安seo学院
  • CSS3网站开发图片外链生成
  • 百度爱采购官方网站公司网站如何seo
  • 可以做渗透的网站如何自己创造一个网站平台
  • 网站做效果图流程百度百家号官网
  • 做调查问卷的网站可靠吗torrent种子搜索引擎
  • 有哪个网站可以做ppt赚钱长春刚刚最新消息今天
  • 江门模板开发建站网络营销专业如何
  • 怎样做模具钢网站无线网络优化工程师
  • Wix做的网站在国内打不开广西seo快速排名
  • 栾城区城乡建设局网站重庆公司seo
  • 网站建设人员叫什么南京网站排名提升
  • 有了源码怎么做网站网站超级外链