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的情况下

 public class Solution
{
public int[] twoSum(int[] numbers, int target)
{
int left = 0, right = numbers.length-1;
int res[] = new int[2]; while(left < right)
{
// find two numbers
if((numbers[left] + numbers[right]) == target)
{
res[0] = left + 1;
res[1] = right + 1;
break;
}
else if((numbers[left] + numbers[right]) > target) // if greater, right moves to left 1 place
right--;
else // if less, left moves to right 1 place
left++; } return res;
}
}

参考资料:

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

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

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

  1. [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 the ...

  2. [LeetCode] 167. Two Sum II - Input array is sorted 两数和 II - 输入是有序的数组

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

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

    给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数.函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2.请注意,返回的下标值(i ...

  4. 167. Two Sum II - Input array is sorted两数之和

    1. 原始题目 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2. 说明 ...

  5. 29. leetcode 167. Two Sum II - Input array is sorted

    167. Two Sum II - Input array is sorted Given an array of integers that is already sorted in ascendi ...

  6. LeetCode 167 Two Sum II - Input array is sorted

    Problem: Given an array of integers that is already sorted in ascending order, find two numbers such ...

  7. (双指针 二分) 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 the ...

  8. ✡ leetcode 167. Two Sum II - Input array is sorted 求两数相加等于一个数的位置 --------- java

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

  9. 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 th ...

随机推荐

  1. python 浅析对return的理解

    最近很忙,但是还是很认真的学习python这个东西,不是出于什么目的,只是单纯的喜欢罢了.最近学习的东西比较简单,但是也遇到了一些问题,就是比较迷惑人的问题,今天小编就在这里讲讲自己的对return的 ...

  2. Linux 环境下 MySQ导入和导出MySQL的sql文件

    将服务器上的文件导入或导出还需要使用工具传输到本机中,推荐使用winscp,与xshell搭配使用 1 导入数据库 两种方法 .首先建空数据库 mysql>create database abc ...

  3. 一步一步带你做WebApi迁移ASP.NET Core2.0

    随着ASP.NET Core 2.0发布之后,原先运行在Windows IIS中的ASP.NET WebApi站点,就可以跨平台运行在Linux中.我们有必要先说一下ASP.NET Core. ASP ...

  4. ASP.NET Core 2.0 使用支付宝PC网站支付

    前言 最近在使用ASP.NET Core来进行开发,刚好有个接入支付宝支付的需求,百度了一下没找到相关的资料,看了官方的SDK以及Demo都还是.NET Framework的,所以就先根据官方SDK的 ...

  5. Ubuntu 安装 SQL Server

    SQL Server现在可以在Linux上运行了!正如微软CEO Satya Nadella说的,"Microsoft Loves Linux",既Windows 10内置的Lin ...

  6. TETELaser Cutting System 不连续吹起的问题

    TETELaser Cutting System 不连续吹起的问题    :配置 加工参数-->机器参数-->信号灯和激光器报警:黄灯索引==EX14   红灯索引==EX15 绿灯索引= ...

  7. uva12519

    The Farnsworth Parabox Professor Farnsworth, a renowned scientist that lives in year 3000 working at ...

  8. Reverse Words in a String III

    Given a string, you need to reverse the order of characters in each word within a sentence while sti ...

  9. Button标签自动刷新问题

    在form表单中,button标签在IE浏览器 type类型默认是button ,而在其他浏览器默认是submit. 解决方法1: 设置类型type="button" <bu ...

  10. mysql error 1130 hy000:Host 'localhost' is not allowed to connect to this mysql server 解决方案

    ERROR 1130 (HY000): Host 'localhost' is not allowed to connect to this MySQL server D:\Wamp\mysql-\b ...