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.

Idea 1. two pointer scan

Time complexity: O(n)

Space complexity: O(1)

Idea 2. hashMap

Time Complexity: O(n)

Space complexity: O(n)

Idea 3. binary Search

Time Complexity: O(nlgn)

Space complexity: O(n)

 class Solution {
public int[] twoSum(int[] numbers, int target) {
for(int left = 0, right = numbers.length-1; left < right; ) {
int sum = numbers[left] + numbers[right];
if(sum == target) {
return new int[] {left + 1, right + 1};
}
else if(sum < target) {
++left;
}
else {
--right;
}
} return new int[]{0, 0};
}
}

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

  1. 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], ...

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

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

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

  5. Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)

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

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

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

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

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

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

  9. [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. CRTD模拟MFG工单进行绑定优化

    需求:按单按库生产的CRTD状态半成品工单重复创建问题 绑定成功案例: SELECT DEMANDLINEID,SUPPLYORDERID,DEMANDORDERID,QTYALLOCATED,ITE ...

  2. smtp扫描

    nc扫描 nc -nv ip号 25 nmap扫描

  3. PHPActiveRecord validates

    validates_presence_of #检测是不是为空 为空的话可以抛出异常 *Model类: static $validates_presence_of = array( array('tit ...

  4. 【pycharm】使用过程的相关问题

    背景:安装scrapy后在cmd里可以正常import scrapy模块,但是在pycharm里不可以(python2.7) 问题:cmd中能正常导入模块,在pycharm报错 原因:pycharm里 ...

  5. CRM销售管理功能

    联系项目project:-------是一个大的项目,比如通知开会之类 每个坐席需要分配自己的联系任务,每个联系任务,有自己的完成未完成状态.同时关联着通话记录等 销售计划----销售项目 销售流程: ...

  6. npm run dev 自动打开浏览器

    修改配置: config - index.js - autoOpenBrowser: true

  7. as3.0两点之间简单的运动,斜着运动,任意两点

    import flash.utils.Timer;import flash.events.TimerEvent;//fixed结束点//sprite初始点var fixedX:Number = fix ...

  8. ucore-lab1-练习1report

    练习1 report 问题1:OS镜像文件ucore.img是如何一步一步生成的(需要比较详细地解释Makefile中的每一条相关命令和命令参数的含义,以及说明命令导致的结果)? GNU make是一 ...

  9. Linux 向文件末尾追加命令

    Linux 向文件末尾追加命令 //echo后边用单引号包围要添加的内容 echo 'add content'>>/home/data/test.sh 注意:>> 是追加 ec ...

  10. 关于 No buffer space available (maximum connections reached?): connect 的处理

    一.问题: hudson一个应用打包部署一直不成功,检查报错 检查项目的JOB配置,开始以为是SVN的问题,但是重启SVN后问题一直存在 二.分析: TCP协议中,关闭TCP连接的是Server端(当 ...