167:两数之和 II - 输入有序数组
This commit is contained in:
parent
6c178c7f4b
commit
bed6e0f70f
@ -0,0 +1,72 @@
|
||||
//给定一个已按照 升序排列 的整数数组 numbers ,请你从数组中找出两个数满足相加之和等于目标数 target 。
|
||||
//
|
||||
// 函数应该以长度为 2 的整数数组的形式返回这两个数的下标值。numbers 的下标 从 1 开始计数 ,所以答案数组应当满足 1 <= answer[0]
|
||||
// < answer[1] <= numbers.length 。
|
||||
//
|
||||
// 你可以假设每个输入只对应唯一的答案,而且你不可以重复使用相同的元素。
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//输入:numbers = [2,7,11,15], target = 9
|
||||
//输出:[1,2]
|
||||
//解释:2 与 7 之和等于目标数 9 。因此 index1 = 1, index2 = 2 。
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入:numbers = [2,3,4], target = 6
|
||||
//输出:[1,3]
|
||||
//
|
||||
//
|
||||
// 示例 3:
|
||||
//
|
||||
//
|
||||
//输入:numbers = [-1,0], target = -1
|
||||
//输出:[1,2]
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 2 <= numbers.length <= 3 * 104
|
||||
// -1000 <= numbers[i] <= 1000
|
||||
// numbers 按 递增顺序 排列
|
||||
// -1000 <= target <= 1000
|
||||
// 仅存在一个有效答案
|
||||
//
|
||||
// Related Topics 数组 双指针 二分查找
|
||||
// 👍 533 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
//167:两数之和 II - 输入有序数组
|
||||
class TwoSumIiInputArrayIsSorted{
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new TwoSumIiInputArrayIsSorted().new Solution();
|
||||
}
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int[] twoSum(int[] numbers, int target) {
|
||||
int start = 0;
|
||||
int end = numbers.length-1;
|
||||
while(start<end){
|
||||
if(numbers[start]+numbers[end]==target){
|
||||
return new int[]{start+1,end+1};
|
||||
}else if (numbers[start]+numbers[end]>target){
|
||||
end--;
|
||||
}else{
|
||||
start++;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
<p>给定一个已按照<strong><em> </em>升序排列 </strong>的整数数组 <code>numbers</code> ,请你从数组中找出两个数满足相加之和等于目标数 <code>target</code> 。</p>
|
||||
|
||||
<p>函数应该以长度为 <code>2</code> 的整数数组的形式返回这两个数的下标值<em>。</em><code>numbers</code> 的下标 <strong>从 1 开始计数</strong> ,所以答案数组应当满足 <code>1 <= answer[0] < answer[1] <= numbers.length</code> 。</p>
|
||||
|
||||
<p>你可以假设每个输入只对应唯一的答案,而且你不可以重复使用相同的元素。</p>
|
||||
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>numbers = [2,7,11,15], target = 9
|
||||
<strong>输出:</strong>[1,2]
|
||||
<strong>解释:</strong>2 与 7 之和等于目标数 9 。因此 index1 = 1, index2 = 2 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>numbers = [2,3,4], target = 6
|
||||
<strong>输出:</strong>[1,3]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>numbers = [-1,0], target = -1
|
||||
<strong>输出:</strong>[1,2]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= numbers.length <= 3 * 10<sup>4</sup></code></li>
|
||||
<li><code>-1000 <= numbers[i] <= 1000</code></li>
|
||||
<li><code>numbers</code> 按 <strong>递增顺序</strong> 排列</li>
|
||||
<li><code>-1000 <= target <= 1000</code></li>
|
||||
<li>仅存在一个有效答案</li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>数组</li><li>双指针</li><li>二分查找</li></div></div>\n<div><li>👍 533</li><li>👎 0</li></div>
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user