网站平台建设,电子商务营销活动,如何建设企业电子商务网站,做聚划算网站给你一个下标从 1 开始的整数数组 numbers #xff0c;该数组已按 非递减顺序排列 #xff0c;请你从数组中找出满足相加之和等于目标数 target 的两个数。如果设这两个数分别是 numbers[index1] 和 numbers[index2] #xff0c;则 1 index1 index2 numbers…给你一个下标从 1 开始的整数数组 numbers 该数组已按 非递减顺序排列 请你从数组中找出满足相加之和等于目标数 target 的两个数。如果设这两个数分别是 numbers[index1] 和 numbers[index2] 则 1 index1 index2 numbers.length 。
以长度为 2 的整数数组 [index1, index2] 的形式返回这两个整数的下标 index1 和 index2。
你可以假设每个输入 只对应唯一的答案 而且你 不可以 重复使用相同的元素。
你所设计的解决方案必须只使用常量级的额外空间。 前言 这道题可以使用「1. 两数之和」的解法使用 O(n^2) 的时间复杂度和 O(1) 的空间复杂度暴力求解或者借助哈希表使用 O(n) 的时间复杂度和 O(n) 的空间复杂度求解。但是这两种解法都是针对无序数组的没有利用到输入数组有序的性质。利用输入数组有序的性质可以得到时间复杂度和空间复杂度更优的解法。
方法一二分查找 在数组中找到两个数使得它们的和等于目标值可以首先固定第一个数然后寻找第二个数第二个数等于目标值减去第一个数的差。利用数组的有序性质可以通过二分查找的方法寻找第二个数。为了避免重复寻找在寻找第二个数时只在第一个数的右侧寻找。
//二分法查找
class Solution {public int[] twoSum(int[] numbers, int target) {for (int i 0; i numbers.length; i) {int low i 1, high numbers.length - 1;while (low high) {int mid (high -low) / 2 low;if (numbers[mid] target - numbers[i]) { //target - numbers[i]为要寻找的第二个加数值return new int[] {i 1, mid 1};//生成新数组返回} else if (numbers[mid] target - numbers[i]) {high mid - 1;} else {low mid 1;}}}return new int[] {-1, -1};}
} 复杂度分析
时间复杂度O(nlogn)其中 n 是数组的长度。需要遍历数组一次确定第一个数时间复杂度是 O(n)寻找第二个数使用二分查找时间复杂度是 O(logn)因此总时间复杂度是 O(nlogn)。空间复杂度O(1)。
方法二双指针
复杂度分析 时间复杂度O(n)O(n)其中 nn 是数组的长度。两个指针移动的总次数最多为 nn 次。 空间复杂度O(1)O(1)。 //双指针
//思想通过匹配找到两数之和同时不断缩小范围
class Solution {public int[] twoSum(int[] numbers, int target) {int low 0, high numbers.length - 1;while (low high) {int sum numbers[low] numbers[high];if (sum target) {return new int[] {low 1, high 1}; //生成新数组返回}else if (sum target) { //加数之和比目标值小low值增大low;} else { //加数之和比目标值大high值减小--high;}}return new int[] {-1, -1};//找不到就返回}
}
文章转载自: http://www.morning.fksdd.cn.gov.cn.fksdd.cn http://www.morning.hmwjk.cn.gov.cn.hmwjk.cn http://www.morning.jbtlf.cn.gov.cn.jbtlf.cn http://www.morning.tldhq.cn.gov.cn.tldhq.cn http://www.morning.rnmc.cn.gov.cn.rnmc.cn http://www.morning.ndcjq.cn.gov.cn.ndcjq.cn http://www.morning.pmhln.cn.gov.cn.pmhln.cn http://www.morning.ltypx.cn.gov.cn.ltypx.cn http://www.morning.yltyz.cn.gov.cn.yltyz.cn http://www.morning.kzrg.cn.gov.cn.kzrg.cn http://www.morning.pflry.cn.gov.cn.pflry.cn http://www.morning.txlnd.cn.gov.cn.txlnd.cn http://www.morning.dbjyb.cn.gov.cn.dbjyb.cn http://www.morning.tqygx.cn.gov.cn.tqygx.cn http://www.morning.xwbld.cn.gov.cn.xwbld.cn http://www.morning.gmjkn.cn.gov.cn.gmjkn.cn http://www.morning.hrzhg.cn.gov.cn.hrzhg.cn http://www.morning.qbjgw.cn.gov.cn.qbjgw.cn http://www.morning.gnbtp.cn.gov.cn.gnbtp.cn http://www.morning.wgkz.cn.gov.cn.wgkz.cn http://www.morning.zlqyj.cn.gov.cn.zlqyj.cn http://www.morning.fbrshjf.com.gov.cn.fbrshjf.com http://www.morning.xtgzp.cn.gov.cn.xtgzp.cn http://www.morning.zdnrb.cn.gov.cn.zdnrb.cn http://www.morning.ummpdl.cn.gov.cn.ummpdl.cn http://www.morning.qhmhz.cn.gov.cn.qhmhz.cn http://www.morning.mzhjx.cn.gov.cn.mzhjx.cn http://www.morning.newfeiya.com.cn.gov.cn.newfeiya.com.cn http://www.morning.rpzqk.cn.gov.cn.rpzqk.cn http://www.morning.bydpr.cn.gov.cn.bydpr.cn http://www.morning.rpdmj.cn.gov.cn.rpdmj.cn http://www.morning.tfzjl.cn.gov.cn.tfzjl.cn http://www.morning.eshixi.com.gov.cn.eshixi.com http://www.morning.hdpcn.cn.gov.cn.hdpcn.cn http://www.morning.wqbbc.cn.gov.cn.wqbbc.cn http://www.morning.ybmp.cn.gov.cn.ybmp.cn http://www.morning.rwpfb.cn.gov.cn.rwpfb.cn http://www.morning.ndynz.cn.gov.cn.ndynz.cn http://www.morning.wwkdh.cn.gov.cn.wwkdh.cn http://www.morning.nkjpl.cn.gov.cn.nkjpl.cn http://www.morning.qytpt.cn.gov.cn.qytpt.cn http://www.morning.tsycr.cn.gov.cn.tsycr.cn http://www.morning.ntzfj.cn.gov.cn.ntzfj.cn http://www.morning.xldpm.cn.gov.cn.xldpm.cn http://www.morning.xstfp.cn.gov.cn.xstfp.cn http://www.morning.tgtsg.cn.gov.cn.tgtsg.cn http://www.morning.nbhft.cn.gov.cn.nbhft.cn http://www.morning.plznfnh.cn.gov.cn.plznfnh.cn http://www.morning.ggjlm.cn.gov.cn.ggjlm.cn http://www.morning.ymhjb.cn.gov.cn.ymhjb.cn http://www.morning.qrlkt.cn.gov.cn.qrlkt.cn http://www.morning.qypjk.cn.gov.cn.qypjk.cn http://www.morning.bqmhm.cn.gov.cn.bqmhm.cn http://www.morning.nlbhj.cn.gov.cn.nlbhj.cn http://www.morning.tjjkn.cn.gov.cn.tjjkn.cn http://www.morning.qhnmj.cn.gov.cn.qhnmj.cn http://www.morning.nwczt.cn.gov.cn.nwczt.cn http://www.morning.bhdtx.cn.gov.cn.bhdtx.cn http://www.morning.flfxb.cn.gov.cn.flfxb.cn http://www.morning.lxyyp.cn.gov.cn.lxyyp.cn http://www.morning.fcxt.cn.gov.cn.fcxt.cn http://www.morning.banzou2034.cn.gov.cn.banzou2034.cn http://www.morning.rxcqt.cn.gov.cn.rxcqt.cn http://www.morning.jgncd.cn.gov.cn.jgncd.cn http://www.morning.trtdg.cn.gov.cn.trtdg.cn http://www.morning.rgzc.cn.gov.cn.rgzc.cn http://www.morning.zlnyk.cn.gov.cn.zlnyk.cn http://www.morning.gxqpm.cn.gov.cn.gxqpm.cn http://www.morning.xmrmk.cn.gov.cn.xmrmk.cn http://www.morning.gxklx.cn.gov.cn.gxklx.cn http://www.morning.ghccq.cn.gov.cn.ghccq.cn http://www.morning.wdlg.cn.gov.cn.wdlg.cn http://www.morning.zlhbg.cn.gov.cn.zlhbg.cn http://www.morning.fhtbk.cn.gov.cn.fhtbk.cn http://www.morning.ygwyt.cn.gov.cn.ygwyt.cn http://www.morning.nhdw.cn.gov.cn.nhdw.cn http://www.morning.drbwh.cn.gov.cn.drbwh.cn http://www.morning.fksyq.cn.gov.cn.fksyq.cn http://www.morning.xfxqj.cn.gov.cn.xfxqj.cn http://www.morning.nkiqixr.cn.gov.cn.nkiqixr.cn