Two Sum II - Input array is sorted LT167
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的更多相关文章
- 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], ...
- 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 ...
- 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 ...
- 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 ...
- Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)
Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted) 给定一个已按照升序排列 的有序数组,找到两个数使得它们 ...
- 【LEETCODE】38、167题,Two Sum II - Input array is sorted
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 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 ...
- 167. Two Sum II - Input array is sorted - LeetCode
Question 167. Two Sum II - Input array is sorted Solution 题目大意:和Two Sum一样,这里给出的数组是有序的 思路:target - nu ...
- [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 ...
随机推荐
- 第七篇:Jmeter连接MySQL的测试
.准备一个有数据表格的MySQL数据库: 2.在测试计划面板上点击浏览按钮,把你的JDBC驱动添加进来: mysql-connector-java-5.1.26-bin.jar 3.添加一个线程组-- ...
- python reload(sys) 后无法输出
重新加载sys后,在idle里无法输出内容.出现这种情况,需要将sys的三个变量重新定义. 在重新载入sys之前,为三个变量赋值 a,b,c = sys.studin,sys.studout,sys. ...
- JMeter (二十)参数化、检查点、集合点(转载)
转载自 http://www.cnblogs.com/yangxia-test 参数化:简单的来理解一下,我们录制了一个脚本,这个脚本中有登录操作,需要输入用户名和密码,假如系统不允许相同的用户名和密 ...
- 织梦 列表页 list标签 按照自已设置的方式排序
一.可以按照权重排序 降序排序 desc 1.添加的文章默认权重是自动加1,所以只要把想置顶的文章权重设置很高,如10000 2.{dede:list pagesize='12′ orderby='w ...
- 筛素数 poj 2739
题目链接:https://vjudge.net/problem/POJ-2739 输入一个数字n,判断有没有一段连续的素数之和大于n,如果有,计算总共有几种. 思路:用素数筛法求出10000以内的素数 ...
- Appium+python自动化1-环境搭建
一.前言 appium可以说是做app最火的一个自动化框架,它的主要优势是支持android和ios,另外脚本语言也是支持java和Python.小编擅长Python,所以接下来的教程是appium+ ...
- java函数方法
1.方法重载 (1)源代码 // MethodOverload.java // Using overloaded methods public class MethodOverload { publi ...
- f5电源模块损坏
现象: ssh登录f5后有日志显示 现场确认f5 2槽电源指示灯不亮,且电源线正常.重新开关电源模块,拔插电源线后仍不亮.故确认是电源模块损坏. 处理:直接采购新的电源模块更换即可.f5电源支持热插拔 ...
- mysql 主从数据不一致 Slave_SQL_Running: No 解决方法
在slave服务器上通过如下命令 mysql> show slave status\G; 显示如下情况: Slave_IO_Running: Yes Slave_SQL_Running: No ...
- gearman中任务的优先级和返回状态
gearman中任务的优先级和返回状态 一.任务的优先级 同步阻塞调用,等待返回结果 doLow:最低优先 doNomal:正常优先级 doHigh:最优先执行 异步派发任务,不等待返回结果,返回任务 ...