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. 为bookStore添加权限【动态代理和注解】

    前言 目前为止,我们已经学习了动态代理技术和注解技术了.于是我们想要为之前的bookStore项目添加权限控制-.. 只有用户有权限的时候,后台管理才可以进行相对应的操作-.. 实现思路 之前我们做权 ...

  2. 利用Struts拦截器限制上传图片的格式和大小

    在这之前 Struts的一个核心功能就是大量的拦截器,既然是框架,那么自然也就贴心地为我们准备好了各种常用的功能,比如这里即将讨论的如何限制上传图片的格式和大小.那么既然是使用Struts已经写好的拦 ...

  3. JPA关系映射之many-to-many

    @ManyToMany Board表实体类 @Entity @Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) @Table(nam ...

  4. uvalive 3971 Assemble

    https://vjudge.net/problem/UVALive-3971 题意: 现在你要组装一台电脑,每个电脑的一种类型的配件都有多种选择,它们的名字是不同的. 现在给出已有的元件,每种类型都 ...

  5. Spring+SpringMVC+MyBatis整合进阶篇(四)RESTful实战(前端代码修改)

    前言 前文<RESTful API实战笔记(接口设计及Java后端实现)>中介绍了RESTful中后端开发的实现,主要是接口地址修改和返回数据的格式及规范的修改,本文则简单介绍一下,RES ...

  6. js 操作数组(过滤对应数据)

    过滤掉相应数据 var fileList = { "85968439868a92": [{name: 'food.jpeg'}, {name: 'ood.jpeg'}], &quo ...

  7. 彻底弄懂AngularJS中的transclusion

    点击查看AngularJS系列目录 彻底弄懂AngularJS中的transclusion AngularJS中指令的重要性是不言而喻的,指令让我们可以创建自己的HTML标记,它将自定义元素变成了一个 ...

  8. 我的第一个python web开发框架(2)——一个简单的小外包

    第一部分说明 第一部分大概有20来章,主要讲的是一些开发常识.开发前中后期准备内容.开发环境与服务器部署环境安装设置.python基础框架结构与功能等内容,代码会比较简单. 本系列会以故事的方式,向大 ...

  9. Egg + Vue 服务端渲染工程化实现

    在实现 egg + vue 服务端渲染工程化实现之前,我们先来看看前面两篇关于Webpack构建和Egg的文章: 在 Webpack工程化解决方案easywebpack 文章中我们提到了基于 Vue ...

  10. 基于C#的BarCode 39实现

    一.39条码简介 39码是1974年发展出来的条码系统,是一种可供使用者双向扫瞄的分散式条码,也就是说相临两资料码之间,必须包含一个不具任何意义的空白(或细白,其逻辑值为0),且其具有支援文字的能力, ...