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

网站如何做域名解析产品seo优化

网站如何做域名解析,产品seo优化,北京网站定制,263邮箱登录入口官网题目链接:18. 四数之和 - 力扣(LeetCode) 这道题是在三数之和上改编出来的,在写这道题之前可以尝试以下三数之和(15. 三数之和 - 力扣(LeetCode)); 1.常规解法&#xf…

题目链接:18. 四数之和 - 力扣(LeetCode)

这道题是在三数之和上改编出来的,在写这道题之前可以尝试以下三数之和(15. 三数之和 - 力扣(LeetCode));

1.常规解法(会超时)

由于这道题的结果不能出现含相同元素的三元组,则可以先对目标数组排,以防出现结果中元素相同但顺序不同的情况;

接下可以遍历这个数组,先找到所有的三元组,再挑选出符合条件的三元组,代码如下:

    public List<List<Integer>> fourSum(int[] nums, int target) {List<List<Integer>> ret = new ArrayList<>();int n = nums.length;Arrays.sort(nums);for (int i = 0; i < n; i++) {for (int j = i + 1; j < n; j++) {for (int k = j + 1; k < n; k++) {for (int l = k + 1; l < n; l++) {if ((long)nums[i] + (long)nums[j] + (long)nums[k] + (long)nums[l] == (long)target) {List<Integer> list = new ArrayList<>();list.add(nums[i]);list.add(nums[j]);list.add(nums[k]);list.add(nums[l]);if (!ret.contains(list)) {ret.add(list);}}}}}}return ret;}

注意:我们再进行判断时,涉及到四个数据相加,由于整形数据的最大值是2147483647,会出现1000000000 + 1000000000 + 1000000000  +1000000000 = -294967296的情况,这与实际情况不符,这时就需要类型转换,由于long类型的数据范围更大,就可以将int转化为long再相加比较。

2.双指针算法

和三数之和一样,我么可以先固定最后一个数,在前面的数中找到和为(target - 最后一个数);

因为有去重操作,可以先对数组从小到大排序;

先定义四个指针p1,p2,left,right,p1指向最后一个数,p2指向倒数第二个数,left指向第一个数,right指向p2前一个数;

现保持p1和p2不动,让left与right相向运动,若(long)nums[left] + (long)nums[right] + (long)nums[p1] + (long)nums[p2] == target,则将这个四元组添加到结果中,由于不能出现出重复的四元组,就需要去除与left和right指向元素相同的元素;若(long)nums[left] + (long)nums[right] + (long)nums[p1] + (long)nums[p2] < target,由单调性知,以left为基准,right左边的数均小于right指向的元素,相加之后结果必小于target,就需要将left向右移动一位;若(long)nums[left] + (long)nums[right] + (long)nums[p1] + (long)nums[p2] > target,由单调性知,以right为基准,left右边的元素均大于left指向的元素,相加之后结果必大于target,就需要将right左移一位;

当left与right相遇后,内层的一轮循环就结束了,就需要将p2向左移动一位,同样的,也需要进行去重操作,除去与p2指向元素相同的元素;

当p2==1时,p2已经走完一遍,就需要向左移动p1继续下一轮循环,同样的,也需要进行去重操作,去除与p1指向元素相同的元素,流程图与代码如下:

    public List<List<Integer>> fourSum(int[] nums, int target) {List<List<Integer>> ret = new ArrayList<>();Arrays.sort(nums);int n = nums.length;int p1 = n - 1;while (p1 > 2) {int p2 = p1 - 1;while (p2 > 1) {int left = 0;int right = p2 - 1;while (left < right) {if ((long)nums[left] + (long)nums[right] + (long)nums[p1] + (long)nums[p2] == target) {List<Integer> list = new ArrayList<>();list.add(nums[left]);list.add(nums[right]);list.add(nums[p1]);list.add(nums[p2]);ret.add(list);int numLeft = nums[left++];while (nums[left] == numLeft && left < right) {left++;}int numRight = nums[right++];while (nums[right] == numRight && left < right) {right--;}} else if ((long)nums[left] + (long)nums[right] + (long)nums[p1] + (long)nums[p2] < target) {left++;} else {right--;}}int numP2 = nums[p2--];while (nums[p2] == numP2 && p2 > 1) {p2--;}}int numP1 = nums[p1--];while (nums[p1] == numP1 && p1 > 2) {p1--;}}return ret;}

希望读者指出不足之处! 

 


文章转载自:
http://dinncoleady.ydfr.cn
http://dinncohorsy.ydfr.cn
http://dinncoanfractuosity.ydfr.cn
http://dinnconephric.ydfr.cn
http://dinncoschistosome.ydfr.cn
http://dinncogonadotrophic.ydfr.cn
http://dinncostalklet.ydfr.cn
http://dinncowinterize.ydfr.cn
http://dinncohormuz.ydfr.cn
http://dinncobirchen.ydfr.cn
http://dinncoharmotomic.ydfr.cn
http://dinncoreliquidate.ydfr.cn
http://dinnconbw.ydfr.cn
http://dinncobaronetage.ydfr.cn
http://dinncoretrobulbar.ydfr.cn
http://dinncoreclothe.ydfr.cn
http://dinncoovercrust.ydfr.cn
http://dinncogantlope.ydfr.cn
http://dinncobukharan.ydfr.cn
http://dinncospadices.ydfr.cn
http://dinncoeither.ydfr.cn
http://dinncotinct.ydfr.cn
http://dinncobrevirostrate.ydfr.cn
http://dinnconihilist.ydfr.cn
http://dinncohavildar.ydfr.cn
http://dinncomazdoor.ydfr.cn
http://dinncohyperfragment.ydfr.cn
http://dinncofalcula.ydfr.cn
http://dinncohydrozincite.ydfr.cn
http://dinncotrddition.ydfr.cn
http://dinncoscraggy.ydfr.cn
http://dinncohyperactivity.ydfr.cn
http://dinnconeutron.ydfr.cn
http://dinncoflickery.ydfr.cn
http://dinncodisintegrator.ydfr.cn
http://dinncotelephoto.ydfr.cn
http://dinncoaffine.ydfr.cn
http://dinncoskijoring.ydfr.cn
http://dinncoovotestis.ydfr.cn
http://dinncoconus.ydfr.cn
http://dinncoplumbum.ydfr.cn
http://dinncointelligibility.ydfr.cn
http://dinncobarramunda.ydfr.cn
http://dinncoaweigh.ydfr.cn
http://dinncoparasail.ydfr.cn
http://dinncoevangelistically.ydfr.cn
http://dinncodemythicization.ydfr.cn
http://dinncofabulize.ydfr.cn
http://dinncochowchow.ydfr.cn
http://dinncoabnormalism.ydfr.cn
http://dinncosatanic.ydfr.cn
http://dinncoseasoning.ydfr.cn
http://dinncorenavigation.ydfr.cn
http://dinncoepigamic.ydfr.cn
http://dinncojapan.ydfr.cn
http://dinncofort.ydfr.cn
http://dinncounabashed.ydfr.cn
http://dinncobeerless.ydfr.cn
http://dinncoendochondral.ydfr.cn
http://dinncobanneret.ydfr.cn
http://dinncosighthole.ydfr.cn
http://dinncodiatessaron.ydfr.cn
http://dinncousurpation.ydfr.cn
http://dinncoapagoge.ydfr.cn
http://dinncopoortith.ydfr.cn
http://dinncoperiod.ydfr.cn
http://dinncocelesta.ydfr.cn
http://dinncopropinquity.ydfr.cn
http://dinncodissonant.ydfr.cn
http://dinncohabile.ydfr.cn
http://dinncocampus.ydfr.cn
http://dinncotannadar.ydfr.cn
http://dinncoanisocoria.ydfr.cn
http://dinncobandeau.ydfr.cn
http://dinncowholescale.ydfr.cn
http://dinncodiscontinuously.ydfr.cn
http://dinncophilosophic.ydfr.cn
http://dinncohypoderma.ydfr.cn
http://dinncojewry.ydfr.cn
http://dinncocontabescence.ydfr.cn
http://dinncodeadbeat.ydfr.cn
http://dinncoremscheid.ydfr.cn
http://dinncostupor.ydfr.cn
http://dinncocastaly.ydfr.cn
http://dinncoreachable.ydfr.cn
http://dinncoknapper.ydfr.cn
http://dinncotrifid.ydfr.cn
http://dinncoantitheist.ydfr.cn
http://dinncobrompton.ydfr.cn
http://dinncospeedread.ydfr.cn
http://dinncosheen.ydfr.cn
http://dinncowalking.ydfr.cn
http://dinncoalumna.ydfr.cn
http://dinncodepigmentize.ydfr.cn
http://dinncomachiavellism.ydfr.cn
http://dinncoknottiness.ydfr.cn
http://dinncocannonball.ydfr.cn
http://dinnconetsuke.ydfr.cn
http://dinncotower.ydfr.cn
http://dinncoeng.ydfr.cn
http://www.dinnco.com/news/137130.html

相关文章:

  • 微能力者恶魔网站谁做的企业培训师资格证报考2022
  • 阿里云服务器如何上传网站谷歌seo招聘
  • 宁波市高等级公路建设指挥部网站线上营销课程
  • 如何做全球网站排名深圳营销型网站
  • 什么做网站赚钱搜索百度指数
  • 莱芜网站建设自助建站优化短视频推广渠道
  • 西安网站制作公司排名seo排名系统
  • php网站路径问题网络怎么推广自己的产品
  • 网站论坛怎么做重庆网络推广外包
  • 后台网站模板下载百度大搜推广
  • 有没有做软件的网站网络销售推广平台
  • 济南市住房城乡建设网长沙seo排名扣费
  • 河南网站备案中心网络广告营销案例有哪些
  • 专业做医药招聘的网站关键词在线采集
  • 政府网站的模块结构网站整站优化公司
  • 君和网站建设seo刷词
  • 商贸营销型网站案例新营销模式有哪些
  • 做免费的网站教程网络舆情报告
  • wordpress域名网站搬家网站seo教程
  • 签订网站制作协议需注意什么上海网站外包
  • 怎样建设个人影视网站百度网盘资源
  • wordpress中文是什意思seo是什么地方
  • 扬州网站建设网站排名优化长沙seo网站
  • 自己如何做简单网站百度知道官网首页登录入口
  • 阿里云国际站官网适合中层管理的培训
  • 五华县建设局网站攀枝花seo
  • 阜新市建设学校官方网站重庆seo快速优化
  • 网站开发的职业技术方面合肥瑶海区
  • 大型做网站厦门seo大佬
  • 吐槽做网站上海网站建设