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 exactly one solution and you may not use the same element 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.
 
思路:
two pointers法:i,j 分别指向sorted array的头和尾,如果numbers[i] + numbers[j] < target, 左指针后移,否则有指针左移。当i = j时,则没有找到和为target对应的index。则返回空。
 
注意:
j = numbers.length - 1; 不要再忘了-1 才是数组最后一位的index!!
 
代码:
    public int[] twoSum(int[] numbers, int target) {

        int i = 0, j = numbers.length - 1;

        while (i != j) {
int sum = numbers[i] + numbers[j];
if (sum == target) {
return new int[]{i+1, j+1};
}
else if (sum > target) {
j--;
}
else {
i++;
}
}
return new int[]{};
}

Leetcode167-Two Sum II Input array is sorted-Easy的更多相关文章

  1. LeetCode167. Two Sum II - Input array is sorted(双指针)

    题意:对于一个有序数组,输出和为target的两个元素的下标.题目保证仅有唯一解. 分析: 法一:二分.枚举第一个元素,二分找另一个元素,时间复杂度O(nlogn),非最优解. class Solut ...

  2. LeetCode_167. Two Sum II - Input array is sorted

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

  3. leetcode2 Two Sum II – Input array is sorted

    Two Sum II – Input array is sorted whowhoha@outlook.com Question: Similar to Question [1. Two Sum], ...

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

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

  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之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)

    Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted) 给定一个已按照升序排列 的有序数组,找到两个数使得它们 ...

  8. 【LEETCODE】38、167题,Two Sum II - Input array is sorted

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

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

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

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

随机推荐

  1. 转:获得数据库自增长ID(ACCESS)与(SQLSERVER)

    转载自:http://www.cnblogs.com/chinahnzl/articles/968649.html 问题CSDN 里面不时有初学者疑惑:如何获取自增长列(标识列)的ID,并写入另一张表 ...

  2. 单台主机上DB2 10.5和arcgis 10.4 空间数据库配置

    该篇文章重点参考arcgis官网说明:http://enterprise.arcgis.com/zh-cn/server/10.4/publish-services/linux/register-db ...

  3. sql语句查询结果排序

    order by 是用在where条件之后,用来对查询结果进行排序 order by 字段名 asc/desc   asc 表示升序(默认为asc,可以省略)     desc表示降序 order b ...

  4. Python爬虫【四】Scrapy+Cookies池抓取新浪微博

    1.设置ROBOTSTXT_OBEY,由true变为false 2.设置DEFAULT_REQUEST_HEADERS,将其改为request headers 3.根据请求链接,发出第一个请求,设置一 ...

  5. java基础之包装类型

    包装类型引入该类型的原因:      因为基本数据类型不具备对象的特性,不能调用方法,所以有时需要将其转换为包装类. 包装类型有两大类方法:      1.将本类型和其它基本类型进行转换方法.    ...

  6. linux下nginx整合php

    在nginx中药使用php可不像apache那样,因为apache是把php当做自己的一个模块来启动的, 而我们的nginx是把http请求转发给php程序,也就是说,php和nginx是相互独立的的 ...

  7. 保存canvas

    http://www.crazybunqnq.com/2018/09/01/PythonSeleniumSaveCanvas/ http://www.webhek.com/post/save-canv ...

  8. django实现类似触发器的效果

    https://blog.csdn.net/pushiqiang/article/details/50652080?utm_source=blogxgwz1 https://blog.csdn.net ...

  9. TCP编程:系统出现 TIME_WAIT 原因及解决办法

    解决办法 打开 sysctl.conf 文件,修改以下几个参数: net.ipv4.tcp_tw_recycle = 1net.ipv4.tcp_tw_reuse = 1net.ipv4.tcp_ti ...

  10. 程序连接oracle数据库问题Cannot create PoolableConnectionFactory ...

    报错: [ERROR] -- :: com.ipi.caee.InitServlet - 初始化数据加载异常 org.springframework.transaction.CannotCreateT ...