题目描述:

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.

Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2

解题思路:

二分法。

代码如下:

public class Solution {
public int[] twoSum(int[] numbers, int target) {
int[] res = new int[2];
int i = 0, j = numbers.length - 1;
while(i < j){
if(numbers[i] + numbers[j] == target){
res[0] = i + 1;
res[1] = j + 1;
break;
} else if(numbers[i] + numbers[j] > target){
j--;
} else{
i++;
}
}
return res;
}
}

  

Java [Leetcode 167]Two Sum II - Input array is sorted的更多相关文章

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

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

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

  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 - O(n) - ( C++ ) - 解题报告

    1.题目大意 Given an array of integers that is already sorted in ascending order, find two numbers such t ...

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

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

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

随机推荐

  1. sonar总结--

    maven的setting.xml 配置  https://www.cnblogs.com/javawebsoa/p/3206504.html

  2. Impala 技术点梳理

    1.优点 1.1  快! 主节点生成执行计划树并分发执行计划至各节点并行执行的拉式获取数据(MR:推式获取数据) 计算的中间结果不写入磁盘 1.2 便利 提供SQL语义,可以方便的进行复杂的数据分析任 ...

  3. CountDownLatch详解

    功能描述 一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待. 常见用法 多个人等一个信号后继续执行操作.例如5个运动员,等一个发令员的枪响. 一个人等多个人的信号. ...

  4. 解决silk-v3-decoder-master转换wav时,百度语音解析问题

    $cur_dir/silk/decoder >& if [ ! -f "$1.pcm" ]; then /usr/local/ffmpeg/bin/ffmpeg -y ...

  5. Art-Template模板引擎(原生写法与简洁写法)

    模板引擎:把js数据转换成html需要的页面,这就是模板引擎需要做的事     • native原生语法     1. 准备数据     2. 把数据转化成html格式的字符串 使用模板引擎 artT ...

  6. Eclemma的安装

    和TestNG安装一致 Help -->Install New Software -->  Add Name: Eclemma Location:http://update.eclemma ...

  7. hdu3863找规律

    先画一下N=2的情况,先手胜,再画一下N=3的情况,先手胜,所以大胆的猜测,无论N=多少,先手胜!! 这也能A真是个奇迹 #include<map> #include<set> ...

  8. Android实现布局控件自定义属性

    一.自定义ViewGroup 1.onMeasure 决定内部View(子View)的宽度和高度,以及自己的宽度和高度 2.onLayout 决定子View放置的位置 3.onTouchEvent 定 ...

  9. VS的 X64下的汇编编译

    参考博客 VS编译64位汇编时报错:error C4235: 使用了非标准扩展: 不支持在此结构上使用“_asm”关键字 在用VS2013编译内联汇编时,报如下错误: 错误    5    error ...

  10. 利用Docker编译Hadoop 3.1.0

    前言 为什么要使用Docker编译,请自行百度 操作系统环境:Centos 6.8 uname -r 内核版本:2.6.32-642.el6.x86_64 除非有把握否则不要在Centos6.8中直接 ...