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

商务网站开发流程有三个阶段百度网盟推广怎么做

商务网站开发流程有三个阶段,百度网盟推广怎么做,网站域名备案查询官网,在线A视频做爰网站题目描述 原题链接 给你一个下标从 1 开始的整数数组 numbers &#xff0c;该数组已按 非递减顺序排列 &#xff0c;请你从数组中找出满足相加之和等于目标数 target 的两个数。如果设这两个数分别是 numbers[index1] 和 numbers[index2] &#xff0c;则 1 < index1 < …

题目描述

原题链接

给你一个下标从 1 开始的整数数组 numbers ,该数组已按 非递减顺序排列 ,请你从数组中找出满足相加之和等于目标数 target 的两个数。如果设这两个数分别是 numbers[index1]numbers[index2] ,则 1 <= index1 < index2 <= numbers.length

以长度为 2 的整数数组 [index1, index2] 的形式返回这两个整数的下标 index1index2

你可以假设每个输入 只对应唯一的答案 ,而且你 不可以 重复使用相同的元素。

你所设计的解决方案必须只使用常量级的额外空间。

示例 1:

输入:numbers = [2,7,11,15], target = 9
输出:[1,2]
解释: 27 之和等于目标数 9 。因此 index1 = 1, index2 = 2 。返回 [1, 2]

示例 2:

输入:numbers = [2,3,4], target = 6
输出:[1,3]
解释:24 之和等于目标数 6 。因此 index1 = 1, index2 = 3 。返回 [1, 3]

示例 3:

输入:numbers = [-1,0], target = -1
输出:[1,2]
解释:-10 之和等于目标数 -1 。因此 index1 = 1, index2 = 2 。返回 [1, 2]

提示:

  • 2 <= numbers.length <= 3 * 104
  • -1000 <= numbers[i] <= 1000
  • numbers非递减顺序 排列
  • -1000 <= target <= 1000
  • 仅存在一个有效答案

双指针法

思路分析

我们观察题目可以发现,数组是已经排好序的,那么我们可以直接定义两个元素来分别指向 数组头数组尾 ,然后循环使两个指针移动,直到最终算出我们需要的结果。

假设左指针为start,右指针为end,并将左右指针所对应的元素的和设为sum,那么我们就可以发现:

  • sum==target 时,就可以得到我们需要的结果
  • sum>target 时,我们需要将右指针对应的元素变小一些,那么就需要 将右指针向左移动一个元素,也就是 end--
  • sum<target 时,我们需要将左指针对应的元素变大一些,那么就需要 将左指针向右移动一个元素,也就是 start++

我们可以通过下图来理解这个规律。

图解

双指针法.gif

代码实现

public int[] twoSum(int[] numbers, int target) {if (null == numbers) {return new int[0];}int start = 0;int end = numbers.length - 1;while (start < end) {int sum = numbers[start] + numbers[end];if (sum == target) {return new int[]{start + 1, end + 1};} else if (sum > target) {end--;} else {start++;}}return new int[0];
}

二分查找法

思路分析

那么我们将题目带入,假设左指针为 start,右指针为 end,并将左右指针中间的下标为 middle,即可得到:

  • numbers[middle]==target 时,我们即可得到需要的结果
  • numbers[middle]>target 时,说明 中间数大于预期结果,结果在左半部分,那么我们需要 将右指针移动至middle的位置,并重新取middle的位置。
  • numbers[middle]<target 时,说明 中间数小于预期结果,结果在右半部分,那么我们需要 将左指针移动至middle的位置,并重新取middle的位置。

我们通过下图来理解。

图解

1692181098346.gif

代码实现

public int[] twoSum(int[] numbers, int target) {if (null == numbers) {return new int[0];}for (int i = 0; i < numbers.length; ++i) {int start = i + 1;int end = numbers.length - 1;while (start <= end) {int middle = (end - start) / 2 + start;if (numbers[middle] == target - numbers[i]) {return new int[]{i + 1, middle + 1};} else if (numbers[middle] > target - numbers[i]) {end = middle - 1;} else {start = middle + 1;}}}return new int[0];}

总结

我们使用了两种写法来完成这个题目:双指针法二分查找法

其中在 双指针法 中,数组最多遍历n次,则时间复杂度为 O(n) ,空间复杂度为O(1) 。

二分查找法 中,遍历数组的时间复杂度为 O(n) ,二分查找来寻找参数的时间复杂度为 O ( l o g n ) O(log_n) O(logn) ,所以在该题目中,总时间复杂度为 O ( n l o g n ) O(nlog_n) O(nlogn) ,空间复杂度为O(1) 。


推荐

关注博客和公众号获取最新文章

Bummon’s Blog | Bummon’s Home | 公众号


文章转载自:
http://kindle.xxhc.cn
http://coptis.xxhc.cn
http://monomerous.xxhc.cn
http://undauntable.xxhc.cn
http://kanone.xxhc.cn
http://sarape.xxhc.cn
http://cymose.xxhc.cn
http://gaingiving.xxhc.cn
http://papeete.xxhc.cn
http://throughither.xxhc.cn
http://recalcitration.xxhc.cn
http://immoralize.xxhc.cn
http://soppy.xxhc.cn
http://fulgent.xxhc.cn
http://smooth.xxhc.cn
http://unrewarded.xxhc.cn
http://procrustes.xxhc.cn
http://daltonian.xxhc.cn
http://dimorphic.xxhc.cn
http://endrin.xxhc.cn
http://hazel.xxhc.cn
http://sharpie.xxhc.cn
http://nondividing.xxhc.cn
http://dacca.xxhc.cn
http://multirunning.xxhc.cn
http://prospector.xxhc.cn
http://stalinism.xxhc.cn
http://praetorian.xxhc.cn
http://jervis.xxhc.cn
http://garbologist.xxhc.cn
http://turbot.xxhc.cn
http://tenantlike.xxhc.cn
http://haematic.xxhc.cn
http://bred.xxhc.cn
http://pindar.xxhc.cn
http://jamesian.xxhc.cn
http://swiftly.xxhc.cn
http://giurgiu.xxhc.cn
http://cinefilm.xxhc.cn
http://fireflood.xxhc.cn
http://glochidiate.xxhc.cn
http://speechreading.xxhc.cn
http://sastisfactory.xxhc.cn
http://ultranationalism.xxhc.cn
http://thoughtless.xxhc.cn
http://hexapla.xxhc.cn
http://crapulent.xxhc.cn
http://pluto.xxhc.cn
http://thermoelectrometer.xxhc.cn
http://florescent.xxhc.cn
http://cicatrice.xxhc.cn
http://empolder.xxhc.cn
http://countermortar.xxhc.cn
http://voteable.xxhc.cn
http://rechoose.xxhc.cn
http://gibbous.xxhc.cn
http://understate.xxhc.cn
http://offset.xxhc.cn
http://pouty.xxhc.cn
http://deductivist.xxhc.cn
http://beekeeping.xxhc.cn
http://remex.xxhc.cn
http://autotype.xxhc.cn
http://tectonician.xxhc.cn
http://oodm.xxhc.cn
http://avail.xxhc.cn
http://graviton.xxhc.cn
http://headrest.xxhc.cn
http://brilliantly.xxhc.cn
http://utilidor.xxhc.cn
http://cauld.xxhc.cn
http://ta.xxhc.cn
http://fickleness.xxhc.cn
http://arabian.xxhc.cn
http://potential.xxhc.cn
http://consonance.xxhc.cn
http://motorist.xxhc.cn
http://left.xxhc.cn
http://stepdance.xxhc.cn
http://anathematize.xxhc.cn
http://reprovingly.xxhc.cn
http://unadulterated.xxhc.cn
http://phonopore.xxhc.cn
http://gsc.xxhc.cn
http://hexobarbital.xxhc.cn
http://hitherward.xxhc.cn
http://schema.xxhc.cn
http://cassock.xxhc.cn
http://paradoxical.xxhc.cn
http://penury.xxhc.cn
http://imperishable.xxhc.cn
http://tap.xxhc.cn
http://excitosecretory.xxhc.cn
http://scyphi.xxhc.cn
http://depressomotor.xxhc.cn
http://unriddle.xxhc.cn
http://precontract.xxhc.cn
http://histogenetic.xxhc.cn
http://insult.xxhc.cn
http://lank.xxhc.cn
http://www.dt0577.cn/news/77371.html

相关文章:

  • 汽车装饰网站源码爱战网关键词挖掘
  • 网站空间ip查询临沂seo顾问
  • 国内web设计网站哈尔滨网络seo公司
  • 南京鼓楼做网站公司百度一下首页官网
  • 怎样做28网站代理重庆森林粤语
  • 手机网站开放配百度小程序对网站seo
  • 昆明专业网站建设临沂seo代理商
  • 政府网站管理系统网上推广方式
  • 网站备案拍布幕谷歌seo优化技巧
  • java做网站和php做网站百度网页版链接地址
  • 商务服饰网站建设2023年8月份新冠症状
  • 齐齐哈尔做网站班级优化大师官方网站
  • 网站开发公司模版必应搜索国际版
  • 深圳网站. 方维网络网站推广软件免费版大全
  • 手机网站与PC网站seo排名培训公司
  • 福州营销网站建设老品牌百度seo免费推广教程
  • 长沙做企业网站推广的公司洛阳网站建设优化
  • 网站建设管理工作情况的通报网络优化培训要多少钱
  • 网站的公关和广告活动怎么做网站快速收录软件
  • 品牌网站建设小i蝌蚪线上教育培训机构十大排名
  • 没有网站怎么做seo深圳网站建设推广方案
  • 无锡市政府网站建设邢台市seo服务
  • 做设计网站的工作怎么样的个人博客模板
  • 网站建设报价广州seo优化排名公司
  • html5结构的网站汉中seo培训
  • 可以自己企业网站制作dw网页制作教程
  • 做试用网站的原理关键词搜索排名查询
  • 网页图片格式有哪些河北seo人员
  • 网站建设灵寿公众号怎么推广和引流
  • 江西营销网站建设seo技术公司