LeetCode 167. Two Sum II - Input array is sorted (两数之和之二 - 输入的是有序数组)

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 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

You may assume that each input would have exactly one solution and you may not use the same element twice.

Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2



题目标签:Array, Two Pointers

  题目给了我们一个array, 和一个 target, array 是递增排列的,让我们找到两个数字 之和 等于 target。 这里要返回的是 它们的index, 不过是从1开始的。

  利用two pointers, 一个left = 0, 一个 right = numbers.length - 1, 因为array 是递增排列的, 所以当 left 数字 + right 数字 大于 target 的话,说明 之和太大了,需要更小的数字,所以把 right-- 来拿到更小的数字;

  当left 数字 + right 数字 小于 target 的话, 说明 之和太小了, 需要更大的数字, 所以要把 left++ 来拿到更大的数字;

  当 left 数字 + right 数字 等于target 的话,直接返回 index + 1。

Java Solution:

Runtime beats 44.34%

完成日期:04/06/2017

关键词:Array, Two Pointers

关键点:了解 两数之和 与 target 的比较下 两个指针该如何移动,建立在递增array的情况下

 1 public class Solution
 2 {
 3     public int[] twoSum(int[] numbers, int target)
 4     {
 5         int left = 0, right = numbers.length-1;
 6         int res[] = new int[2];
 7
 8         while(left < right)
 9         {
10             // find two numbers
11             if((numbers[left] + numbers[right]) == target)
12             {
13                 res[0] = left + 1;
14                 res[1] = right + 1;
15                 break;
16             }
17             else if((numbers[left] + numbers[right]) > target) // if greater, right moves to left 1 place
18                 right--;
19             else // if less, left moves to right 1 place
20                 left++;
21
22         }
23
24         return res;
25     }
26 }

参考资料:

http://www.cnblogs.com/ganganloveu/p/4198968.html

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

时间: 2024-10-06 01:06:11

LeetCode 167. Two Sum II - Input array is sorted (两数之和之二 - 输入的是有序数组)的相关文章

[LeetCode] Two Sum II - Input array is sorted 两数之和之二 - 输入数组有序

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 mu

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. Two Sum II - Input array is sorted两数之和

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

Java [Leetcode 167]Two Sum II - Input array is sorted

题目描述: 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 in

LeetCode - 167. Two Sum II - Input array is sorted - O(n) - ( C++ ) - 解题报告

1.题目大意 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 i

LeetCode 167. Two Sum II – Input array is sorted

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] Two Sum II - Input array is sorted

给定数组排好序了,然后给一个目标,找到两个数相加等于目标的两个数的下标. 蛮简单感觉,就是左右两边往里,比目标大就右边减,小就左边加.一样就输出. vector<int> twoSum(vector<int> &numbers, int target){ vector<int> ans; int left = 1, right = numbers.size(); while(left < right){ if (numbers[left-1] + numb

LeetCode 167. Two Sum II - Input array is sorted(双指针)

题目 题意:找出数组里两个数字之和为指定数字的两个下标. 题解:双指针 class Solution { public: vector<int> twoSum(vector<int>& numbers, int target) { int left = 0; int right = numbers.size()-1; vector<int> ans; while(left < right) { if(numbers[left]+numbers[right]

【LeetCode】167. Two Sum II - Input array is sorted

Two Sum II - Input array is sorted 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 a