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. mysql创建用户和库

    先用root登陆mysql,然后运行下面代码创建mysql用户和库 下面创建的帐号:test123  密码:123456  库名:test123 CREATE USER '; GRANT USAGE ...

  2. ssh X协议转发

    X协议的作用是远程登录Linux运行GUI界面 主机2开启ssh服务service ssh start 主机1 ssh连接主机2:ssh -X root@192.168.1.110 -p 53 然后可 ...

  3. Android Studio 插件-Android Styler 的使用 (转)

    作用:把 xml文件 转为 style 截图保留 使用方法 使用方法:选中xml代码 按下 Ctrl+Shift+D 转自:http://blog.csdn.net/zxwd2015/article/ ...

  4. Android 数据库框架总结(转)

    转自 http://blog.csdn.net/da_caoyuan/article/details/61414626 一:OrmLite 简述: 优点: 1.轻量级:2.使用简单,易上手:3.封装完 ...

  5. 截图软件FastStone

    屏幕截图软件(FastStone Capture) 好用,可以粘贴 / 复制. 可以做页面设计,有屏幕标尺,取色器.

  6. 第九章 词典 (c)散列:散列函数

  7. jdbc java远程连接mysql数据库服务器

    首先,需要注意以下几点: 1.手机需要获得可以访问网络的权限: 2.导入的jdbc驱动的版本需要与mysql服务器的版本相近: 3.mysql默认的访客是只允许本机(localhost),不允许其他主 ...

  8. Unity3d游戏地图生成器MapMagic World Generator v1.9.1

    Unity3d MapMagic World Generator基于节点的程序和无限游戏地图生成器,图形上的每个节点表示地形或对象生成器:噪声,voronoi,混合,曲线,侵蚀,散射,森林等生态系统, ...

  9. Codeforces Beta Round #25 (Div. 2 Only)

    Codeforces Beta Round #25 (Div. 2 Only) http://codeforces.com/contest/25 A #include<bits/stdc++.h ...

  10. label 和input/textarea居中对齐

    设置label和input/textarea的vertical-align: middle;即可实现垂直方向居中对齐.有时候可能会有偏差,设置input的margin-top使看上去居中对齐