题目描述:

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. XML CDATA是什么?

    XML CDATA是什么? 投稿:mdxy-dxy 字体:[增加 减小] 类型:转载   这篇文章主要为大家介绍下XML CDATA是什么,学习xml的朋友可以参考下     All text in ...

  2. Vuejs methods how to use

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  3. Hibernate与 MyBatis的比较(转,留作以后细细钻研)

    最近做了一个Hibernate与MyBatis的对比总结,希望大家指出不对之处. 第一章     Hibernate与MyBatis Hibernate 是当前最流行的O/R mapping框架,它出 ...

  4. Mac下配置NDK环境

    下载NDK 这里写图片描述配置NDK开发环境 第一步:打开Mac终端 Snip20170208_1.png 第二步:在终端中输入:open -e .bash_profile,打开.bash_profi ...

  5. jQuery回车键快速切换下一个input文本框解决方案

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. springmvc+rest整合redis

    最近在做一个项目需要用到关系数据库mysql和缓存redis,以及非关系型数据库mongoDB.昨天下午到今天上午一直在搞springmvc整合redis,期间出现的错误一直让人抓狂,在网上搜索的结果 ...

  7. HTML 参考手册- (HTML5 标准)

    HTML 参考手册- (HTML5 标准) 功能排序 New : HTML5 新标签 标签 描述 基础   <!DOCTYPE>  定义文档类型. <html> 定义一个 HT ...

  8. 如何成为 Python 高手

    这篇文章主要是对我收集的一些文章的摘要.因为已经有很多比我有才华的人写出了大量关于如何成为优秀Python程序员的好文章. 我的总结主要集中在四个基本题目上:函数式编程,性能,测试,编码规范.如果一个 ...

  9. Ngix

    Ngix安装 官网地址,下载为源码,需要编译安装 http://nginx.org/ 环境 1.需要安装gcc的环境. yum install gcc-c++ 2.第三方的开发包. PCRE PCRE ...

  10. 《深入理解mybatis原理4》 MyBatis缓存机制的设计与实现

    <深入理解mybatis原理> MyBatis缓存机制的设计与实现 本文主要讲解MyBatis非常棒的缓存机制的设计原理,给读者们介绍一下MyBatis的缓存机制的轮廓,然后会分别针对缓存 ...