题目

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.

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.

两个指针解法

左右指针两头分别往中间靠近,当然这里还有优化的空间,比如发现两头的和大于目标值,看看left + mid是否也超过,这可以直接折半查找。两头的和小于目标值,看看mid + right 是否也小于,这也可以折半查找。

package binarysearch;
public class TwoSumIIInputArrayIsSorted {public int[] twoSum(int[] numbers, int target) {if (numbers == null || numbers.length < 2) {throw new IllegalArgumentException("numbers array size is less than 2");}int left = 0;int right = numbers.length - 1;while (left < right) {if (numbers[left] + numbers[right] == target) {break;} else if (numbers[left] + numbers[right] > target) {right--;} else {left++;}}return new int[]{left + 1, right + 1};}
}

算法:两个数之和为目标数Two Sum II - Input array is sorted相关推荐

  1. 167. Two Sum II - Input array is sorted 两数之和 II - 输入有序数组

    Title 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2. 说明: ...

  2. 167. Two Sum II - Input array is sorted两数之和

    1. 原始题目 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2. 说明 ...

  3. C#LeetCode刷题之#167-两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3903 访问. 给定一个已按照升序排列 的有序数组,找到两个数使得 ...

  4. 【LeetCode 剑指offer刷题】数组题2:57 有序数组中和为s的两个数(167 Two Sum II - Input array is sorted)...

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 57 有序数组中和为s的两个数 题目描述 输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的他们的和正好是 ...

  5. leetcode 1: 找出两个数相加等于给定数 two sum

    问题描述 对于一个给定的数组,找出2个数,它们满足2个数的和等于一个特定的数,返回这两个数的索引.(从1开始) Given an array of integers, find two numbers ...

  6. 方法:求两个数之和 判断两数是否相等

    package wsq; import java.util.Scanner; public class wsq { public static void main(String[] args) { d ...

  7. 两个数之和等于第三个数

          这是一个很好的算法题,解法类似于快速排序的整理方法.同时,更为值得注意的是这道题是 人人网2014校园招聘的笔试题,下面首先对题目进行描述:       给出一个有序数组,另外给出第三个数 ...

  8. 和数(一个数等于另外两个数之和)

    蒜头君给定一个正整数序列,判断其中有多少个数,等于数列中其他两个数的和.比如,对于数列 1 2 3 4,这个问题的答案就是 2,因为 3 = 2 + 1, 4 = 1 + 3. 输入格式 共两行,第一 ...

  9. 剑指offer之求两个数之和(不能使用四则运算)

    1 题目 剑指offer之求两个数之和(不能使用四则运算) 2 代码实现 #include<stdio.h>int add(int num1, int num2) {int sum1;in ...

  10. 41【C#】斐波那契(Fibonacci)数列的第一个和第二个数分别为1和1 从第三个数开始,每个数等于其前两个数之和(1,1,2,3...)编写一个程序输出斐波那契数列中的前20个数,

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

最新文章

  1. iPhone读取plist文件
  2. Linux_PXE服务器_RHEL7
  3. 基于php的问答,thinkask
  4. 外网服务器搭建网站并获取域名教程
  5. django mysql connector,MySQL Connector / python在Django中不起作用
  6. javascript好文---深入理解定位父级offsetParent及偏移大小
  7. textlayout Java_Java TextLayout.getBounds方法代码示例
  8. input文本框自动填充背景色黄色解决办法
  9. 静态文件用什么服务器配置,静态文件服务器路径怎么配置好
  10. Project Euler Problem 27 Quadratic primes
  11. vs2015编译ffmpeg
  12. 游戏开发之STL库的基础使用(string、vector、list、map、unordered_map)(C++基础)
  13. cpuz测试分数天梯图_最新手机处理器天梯图出炉:第一实至名归,你手机处理器排名高吗...
  14. .NET中获取电脑名、IP地址及用户名方法
  15. Unity3D ----- 制作信息滚动提示(NGUI)
  16. links.php是病毒吗,发现了第一只php病毒PHP.Pirus
  17. 前端如何压缩图片质量
  18. ROS Launch使用总结
  19. Shell编程中的数组定义、遍历
  20. 鱼书P70--mnist.py的导入和应用

热门文章

  1. python版本回退_版本回退
  2. 淘富成真,硬件智能—— 硬件创新一站赋能平台
  3. 那些年陪伴我的老师+我期待的师生关系
  4. 用python写MapReduce函数——以WordCount为例
  5. Linux SocketCan client server demo hacking
  6. 【Deep learning】NLP
  7. tcpdump 的TCP输出结果详解
  8. 【案例】MySQL count操作优化案例一则
  9. .Net环境下有关打印页面设置、打印机设置、打印预览对
  10. VC绘制控件如何防止闪烁