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. Spring-data-redis redis

    什么是spring-data-redis spring-data-redis是spring-data模块的一部分,专门用来支持在spring管理项目对redis的操作,使用java操作redis最常用 ...

  2. 仿造mongodb的存储方式存一些假数据

    //存入数据 $data = json_encode($row); // 过滤 $data = addslashes($data); //读取数据 $falseData = stripslashes( ...

  3. AD操作

    加泪滴 批量添加覆铜过孔(先铺铜以后,再批量添加过孔) 开槽   在KEPP—OUT层 部分区域 不敷铜 开窗  

  4. Vue Baidu Map 插件的使用

    最近在做一个项目,技术采用的是Vue.js套餐,有个百度地图的需求,当时,大脑宕机,立马去引入百度地图API,当时想到两种方法,一种是在index.html中全局引入js,此法吾不喜,就采用了第二种异 ...

  5. TZOJ 1221 Tempter of the Bone(回溯+剪枝)

    描述 The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked i ...

  6. 【转】python中的字符数字之间的转换函数

    int(x [,base ])         将x转换为一个整数     long(x [,base ])        将x转换为一个长整数     float(x )               ...

  7. 交叉编译libudev

    一.交叉编译libudev下载udev-182.tar.xz 下载网址:https://mirrors.edge.kernel.org/pub/linux/utils/kernel/hotplug/ ...

  8. 同种类型不同名字的变量在for循环中操作

    InfoViewController * info = [[InfoViewController alloc] init]; HeroViewController * hero = [[HeroVie ...

  9. 永久激活win和office

    1.关闭自己安装的防护软件 2. 关闭电脑自带的防护软件 3.运行 KMSpico

  10. YII2中查询生成器Query()的使用

    YII2中的yii\db\Query给我们提供了非常丰富的方法,方便我们构建复杂的SQL语句. Query()与createCommand最大区别在于,后者直接使用我们写好的SQL语句,前者通过参数和 ...