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 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 and you may not use the same element twice.

Input: numbers={2, 7, 11, 15}, target=9

Output: index1=1, index2=2

思路:从两头向中间找,如果两头之和比target大,那么肯定要把尾往前移,如果两头之和比target小,那么肯定要把头往后移。

29. leetcode 167. Two Sum II - Input array is sorted的更多相关文章

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

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

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

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

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

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

  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. 题解 最优的挤奶方案(Optimal Milking)

    最优的挤奶方案(Optimal Milking) 时间限制: 1 Sec  内存限制: 128 MB 题目描述 农场主 John 将他的 K(1≤K≤30)个挤奶器运到牧场,在那里有 C(1≤C≤20 ...

  2. 【转载】Sublime Text 3065 Keygen and Patcher

    原始日期:2014-10-01 18:25      差不多时隔一年了,Sublime Text 终于更新啦!相信很多友友都已经升级到3065版本了,所以我也特地抽空为大家做了个新版补丁.该补丁仅作为 ...

  3. PostgreSQL数据库web维护客户端工具软件

    TreeSoft数据库管理系统使用JAVA开发,采用稳定通用的springMVC +JDBC架构,实现基于WEB方式对 MySQL,Oracle,PostgreSQL 等数据库进行维护管理操作. 功能 ...

  4. js对象中动态读取属性值 动态属性值 js正则表达式全局替换

    $(document).ready(function(){ var exceptionMsg = '${exception.message }'; var exceptionstr = ''; //j ...

  5. 【ASP.NET MVC 牛刀小试】 URL Route

    例子引入 先看看如下例子,你能完全明白吗? using System; using System.Collections.Generic; using System.Linq; using Syste ...

  6. Intellij IDEA查看方法的调用栈

    在IDEA中,先双击选定要查看的方法,使用快捷键Ctrl+Alt+h,在右侧就会显示该方法的详细信息,再双击右侧的方法,就定位到方法的代码区.如下图:

  7. [leetcode-556-Next Greater Element III]

    Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly th ...

  8. 【LeetCode】116. Populating Next Right Pointers in Each Node

    题目: Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode ...

  9. 【PHP】制作日历

    本期本博主将讲述两种利用PHP制作日历的方法,由于PHP日期函数的便捷性,使得我们制作日历这一过程变得相当简单 问题描述: 1.取到当前日期,并着色显示:2.根据当前日期,判断本月有多少天,一号是周几 ...

  10. Js作用域&作用域链

    js构建类 一 构建类的原则 构造函数 等于 原型的constructor //构造函数 function Hero(name,skill){ this.name = name; this.skill ...