Difficulty:easy

 More:【目录】LeetCode Java实现

Description

https://leetcode.com/problems/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.

Note:

  • Your returned answers (both index1 and index2) are not zero-based.
  • You may assume that each input would have exactlyone solution and you may not use the sameelement twice.

Example:

Input: numbers = [2,7,11,15], target = 9
Output: [1,2]
Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2.

Intuition

refer to 和为s的两个数字

Solution

    public int[] twoSum(int[] num, int target) {
int i=0, j=num.length-1;
while(i<j){
if(num[i]+num[j]<target)
i++;
else if(num[i]+num[j]>target)
j--;
else
return new int[]{i+1,j+1};
}
return null;
}

  

Complexity

Time complexity : O(n)

Space complexity : O(1)

What I've learned

1.The use of two pointers.

 More:【目录】LeetCode Java实现

【LeetCode】167. Two Sum II - Input array is sorted的更多相关文章

  1. 【LeetCode】167. Two Sum II - Input array is sorted 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

  2. Leetcode No.167 Two Sum II - Input array is sorted(c++实现)

    1. 题目 1.1 英文题目 Given an array of integers numbers that is already sorted in non-decreasing order, fi ...

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

    167. Two Sum II - Input array is sorted[easy] Given an array of integers that is already sorted in a ...

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

    Question 167. Two Sum II - Input array is sorted Solution 题目大意:和Two Sum一样,这里给出的数组是有序的 思路:target - nu ...

  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. 167. Two Sum II - Input array is sorted@python

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

  7. leetcode 1.Two Sum 、167. Two Sum II - Input array is sorted 、15. 3Sum 、16. 3Sum Closest 、 18. 4Sum 、653. Two Sum IV - Input is a BST

    1.two sum 用hash来存储数值和对应的位置索引,通过target-当前值来获得需要的值,然后再hash中寻找 错误代码1: Input:[3,2,4]6Output:[0,0]Expecte ...

  8. [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 ...

  9. LeetCode算法题-Two Sum II - Input array is sorted

    这是悦乐书的第179次更新,第181篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第38题(顺位题号是167).给定已按升序排序的整数数组,找到两个数字,使它们相加到特定 ...

随机推荐

  1. mysql列类型char,varchar,text,tinytext,mediumtext,longtext的比较与选择

    储存不区分大小写的字符数据 TINYTEXT 最大长度是 255 (2^8 – 1) 个字符. TEXT 最大长度是 65535 (2^16 – 1) 个字符. MEDIUMTEXT 最大长度是 16 ...

  2. net.sf.json------json解析

    下载地址 [plain] view plain copy   本次使用版本:http://sourceforge.net/projects/json-lib/files/json-lib/json-l ...

  3. Windows平台上谷歌浏览器损害电池

    From:http://www.cnblogs.com/killerlegend/p/3909208.html Author:KillerLegend Date:2014.8.13 事情是这样的,我的 ...

  4. 设计模式之Mixin模式

    介绍 mixin模式就是一些提供能够被一个或者一组子类简单继承功能的类,意在重用其功能.在面向对象的语言中,我们会通过接口继承的方式来实现功能的复用.但是在javascript中,我们没办法通过接口继 ...

  5. bzoj千题计划196:bzoj4826: [Hnoi2017]影魔

    http://www.lydsy.com/JudgeOnline/problem.php?id=4826 吐槽一下bzoj这道题的排版是真丑... 我还是粘洛谷的题面吧... 提供p1的攻击力:i,j ...

  6. 各种卷积类型Convolution

    从最开始的卷积层,发展至今,卷积已不再是当初的卷积,而是一个研究方向.在反卷积这篇博客中,介绍了一些常见的卷积的关系,本篇博客就是要梳理这些有趣的卷积结构. 阅读本篇博客之前,建议将这篇博客结合在一起 ...

  7. 关于caffe的安装问题

    在caffe的安装过程中,出现 /usr/bin/ld: cannot find -lcblas /usr/bin/ld: cannot find -latlas的问题 这时解决方案为http://s ...

  8. 【干货】Linux内存数据的获取与转存 直捣密码

    知识源:Unit 2: Linux/Unix Acquisition 2.1 Linux/Unix Acquistion Memory Acquisition 中的实验demo部分  小白注意,这是网 ...

  9. innobackupex做MySQL增量备份及恢复【转】

    创建备份用户 mysql> grant process,reload,lock tables,replication client on *.* to 'backup'@'localhost' ...

  10. VS 2013 中如何自定义快捷键(图解)

    随着软件的升级其功能也越来越多,所以相应的快捷键也多了起来.VS2013中的快捷方式已经发展为了两个快捷键的组合.例如VS2013中默认的多行注释的快捷功能键为(ctrl+k,ctrl+u).像这样类 ...