leetcode167. 两数之和 II - 输入有序数组(双指针)

https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted/

求数组中加起来恰好等于target的两个数的位置。

双指针问题,分别从头和尾遍历数组,如果加起来和大于target,则尾指针左移,如果加起来和小于target,则头指针右移,如果恰好则返回数组即可。

注意:1.数组的长度为****.length,没有括号!

2.定义数组new int[]{...}

class Solution {
    public int[] twoSum(int[] numbers, int target) {
        int left=0,right=numbers.length-1;
        while(left<right){
            int sum=numbers[left]+numbers[right];
            if(sum==target)
                return new int[]{left+1,right+1};
            if(sum>target)
                right--;
            else
                left++;
        }
        throw new RuntimeException("在数组中没有找到这样的两个数,使得它们的和为指定值");
    }
}

原文地址:https://www.cnblogs.com/y1040511302/p/11427213.html

时间: 2024-07-31 11:46:33

leetcode167. 两数之和 II - 输入有序数组(双指针)的相关文章

LeetCode167 两数之和 II - 输入有序数组

给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2. 说明: 返回的下标值(index1 和 index2)不是从零开始的. 你可以假设每个输入只对应唯一的答案,而且你不可以重复使用相同的元素. 示例: 输入: numbers = [2, 7, 11, 15], target = 9 输出: [1,2] 解释: 2 与 7 之和等于目标数 9 .因此 index1 = 1

LeetCode【167. 两数之和 II - 输入有序数组】

这道题最开始想到的就是,两个for,target是两数之和,就可以target - numbers[i],比较是否有与后面数相等. class Solution { public int[] twoSum(int[] numbers, int target) { int i,j,s; int c = numbers.length; int[] t = new int[2]; for(i = 0;i <= c-1;i++) { s = target - numbers[i]; t[0] = i+1

167 Two Sum II - Input array is sorted 两数之和 II - 输入有序数组

给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数.函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2.请注意,返回的下标值(index1 和 index2)不是从零开始的.你可以假设每个输入都只有一个解决方案,而且你不会重复使用相同的元素.输入:数组 = {2, 7, 11, 15}, 目标数 = 9输出:index1 = 1, index2 = 2详见:https://leetcode.com/problems/two-

167. 两数之和 II - 输入有序数组

Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 m

Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)

给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2. 说明: 返回的下标值(index1 和 index2)不是从零开始的. 你可以假设每个输入只对应唯一的答案,而且你不可以重复使用相同的元素. 示例: 输入: numbers = [2, 7, 11, 15], target = 9 输出: [1,2] 解释: 2 与 7 之和等于目标数 9 .因此 index1 = 1

两数之和 II - 输入有序数组 --二分查找

题目 给定一个已按照升序排列的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2. 说明: 返回的下标值(index1 和 index2)不是从零开始的. 你可以假设每个输入只对应唯一的答案,而且你不可以重复使用相同的元素. 示例: 输入: numbers = [2, 7, 11, 15], target = 9 输出: [1,2] 解释: 2 与 7 之和等于目标数 9 .因此 index1 =

[LeetCode] Two Sum IV - Input is a BST 两数之和之四 - 输入是二叉搜索树

Given a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum is equal to the given target. Example 1: Input: 5 / 3 6 / \ 2 4 7 Target = 9 Output: True Example 2: Input: 5 / 3 6 / \ 2 4 7 Targe

LeetCode-1两数之和

问题: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 示例: 来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/two-sum著作权归领扣网络所有.商业转载请联系官方授权,非商业转载请注明出处. 分析: 该题核心是查找,那么和查找有关的,我们优先想到的就是通过hashmap来进行.那么通

【数据结构与算法】双指针思想——两数之和

两数之和 II - 输入有序数组 LeetCode:两数之和 II - 输入有序数组 题目描述: 给定一个已按照升序排列?的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1?必须小于?index2. 示例: 输入: numbers = [2, 7, 11, 15], target = 9 输出: [1,2] 解释: 2 与 7 之和等于目标数 9 .因此 index1 = 1, index2 = 2 . 思想: 使用双