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.
  2.  
  3. 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.
  4.  
  5. Note:
  6.  
  7. Your returned answers (both index1 and index2) are not zero-based.
  8. You may assume that each input would have exactly one solution and you may not use the same element twice.
  9. Example:
  10.  
  11. Input: numbers = [2,7,11,15], target = 9
  12. Output: [1,2]
  13. Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2.
  14. Accepted
  15. 210,324
  16. Submissions
  17. 427,592

https://www.cnblogs.com/Roni-i/p/7774100.html

https://www.cnblogs.com/Roni-i/p/9253303.html

几百年前就会双指针了,但是Java有些语法还不熟练。。

(ps:双指针利用序列的递增性

  1. class Solution {
  2. public int[] twoSum(int[] numbers, int target) {
  3. int i = 0, j = numbers.length - 1;
  4. while(i < j) {
  5. int sum = numbers[i] + numbers[j];
  6. if(sum == target) {
  7. return new int[] {i+1, j+1}; //
  8. }
  9. else if(sum > target) {
  10. j--;
  11. }
  12. else {
  13. i++;
  14. }
  15. }
  16. return null;
  17. }
  18. }

167. Two Sum II - Input array is sorted【Easy】【双指针-有序数组求两数之和为目标值的下标】的更多相关文章

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

  2. 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 ...

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

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

  4. 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 ...

  5. 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 ...

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

  7. LeetCode Array Easy 167. Two Sum II - Input array is sorted

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

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

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

随机推荐

  1. npm 淘宝镜像安装以及安装报错window_nt 6.1.7601 解决

    http://www.cnblogs.com/ycxhandsome/p/6562980.html npm config set proxy null npm config set https-pro ...

  2. LightOJ 1028 - Trailing Zeroes (I) 质因数分解/排列组合

    题意:10000组数据 问一个数n[1,1e12] 在k进制下有末尾0的k的个数. 思路:题意很明显,就是求n的因子个数,本来想直接预处理欧拉函数,然后拿它减n就行了.但注意是1e12次方法不可行.而 ...

  3. struts2之OGNL用法

    浅析OGNL OGNL是Object-GraphNavigation Language的缩写,是一种功能强大的表达式语言 通过它简单一致的表达式语法,可以存取对象的任意属性,调用对象的方法,遍历整个对 ...

  4. 【BZOJ】1584: [Usaco2009 Mar]Cleaning Up 打扫卫生

    [算法]DP+数学优化 [题意]把n个1~m的数字分成k段,每段的价值为段内不同数字个数的平方,求最小总价值.n,m,ai<=40000 [题解] 参考自:WerKeyTom_FTD 令f[i] ...

  5. 【洛谷 P3402】 【模板】可持久化并查集

    题目链接 可持久化并查集,就是用可持久化线段树维护每个版本每个节点的父亲,这样显然是不能路径压缩的,否则我们需要恢复太多状态. 但是这并不影响我们启发式合并,于是,每次把深度小的连通块向深度大的上并就 ...

  6. E题hdu 1425 sort

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1425 sort Time Limit: 6000/1000 MS (Java/Others)    M ...

  7. JAVA 非对称加密算法RSA

    非对称加密算法 RSA过程 : 以甲乙双方为例 1.初始化密钥 构建密钥对,生成公钥.私钥保存到keymap中 KeyPairGenerator ---> KeyPair --> RSAP ...

  8. Optimizing TLB entries for mixed page size storage in contiguous memory

    A system and method for accessing memory are provided. The system comprises a lookup buffer for stor ...

  9. LINUX内核面试题摘选

    转载:http://blog.csdn.net/zm1_1zm/article/details/77231197 1) Linux中主要有哪几种内核锁? 答:Linux的同步机制从2.0到2.6以来不 ...

  10. STM32 volatile关键字

    为了提供对特殊地址的稳定访问. [C] 纯文本查看 复制代码 ? 1 2 3 int i=10; int j=i;     //1 int k=i;    //2 此时编译器对上面代码进行优化,因为在 ...