167. Two Sum II - Input array is sorted【Easy】【双指针-有序数组求两数之和为目标值的下标】
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.
Accepted
210,324
Submissions
427,592
https://www.cnblogs.com/Roni-i/p/7774100.html
https://www.cnblogs.com/Roni-i/p/9253303.html
几百年前就会双指针了,但是Java有些语法还不熟练。。
(ps:双指针利用序列的递增性
class Solution {
public int[] twoSum(int[] numbers, int target) {
int i = 0, j = numbers.length - 1;
while(i < j) {
int sum = numbers[i] + numbers[j];
if(sum == target) {
return new int[] {i+1, j+1}; //
}
else if(sum > target) {
j--;
}
else {
i++;
}
}
return null;
}
}
167. Two Sum II - Input array is sorted【Easy】【双指针-有序数组求两数之和为目标值的下标】的更多相关文章
- 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 - LeetCode
Question 167. Two Sum II - Input array is sorted Solution 题目大意:和Two Sum一样,这里给出的数组是有序的 思路:target - nu ...
- 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 1.Two Sum 、167. Two Sum II - Input array is sorted 、15. 3Sum 、16. 3Sum Closest 、 18. 4Sum 、653. Two Sum IV - Input is a BST
1.two sum 用hash来存储数值和对应的位置索引,通过target-当前值来获得需要的值,然后再hash中寻找 错误代码1: Input:[3,2,4]6Output:[0,0]Expecte ...
- [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 ...
- LeetCode Array Easy 167. Two Sum II - Input array is sorted
Description Given an array of integers that is already sorted in ascending order, find two numbers s ...
- LeetCode 167 Two Sum II - Input array is sorted
Problem: Given an array of integers that is already sorted in ascending order, find two numbers such ...
- LeetCode 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 the ...
随机推荐
- MSSQL Get Last Monday and Last Sunday
获取上周的周一和周日 代码: --start of last week , ) --end of last week , )
- 【转载】Java JVM : Xms Xmx PermSize MaxPermSize 区别
转载自:http://cxh61207.iteye.com/blog/1160663 java JVM虚拟机选项: Xms Xmx PermSize MaxPermSize 区别 Xms 是指设定程 ...
- Sass 基本函数
Sass 中的常用函数 一.字符串函数 1. unquote($string): 删除字符串前后的引号,删除一对引号,如果这个字符串没有带有引号,将返回原始的字符串. 示例: .text1 { con ...
- 【BZOJ】2502 清理雪道
[算法]有源汇上下界最小流 [题解]上下界 初看以为是最小覆盖,发现边可以重复经过,不对. 要求所有边都经过……那就下界为1,上界为inf的可行流. 源汇……S连入度为0的点,T连出度为0的点?(反正 ...
- ios资源加载策略
做了好几个月的ios,大框架都是别人搭好的,自己只是实现逻辑,很是失落.慢慢开始整理学习一些概念类的东西吧,希望自己能提高点. cocos2d-x从cocos2d-2.0-x-2.0.2开始,考虑到自 ...
- Atlantis(POJ1151+线段树+扫描线)
题目链接:http://poj.org/problem?id=1151 题目: 题意:求所有矩形的面积,重合部分只算一次. 思路:扫描线入门题,推荐几篇学扫描线的博客: 1.http://www.cn ...
- 编写jquery Plugin
编写jquery插件的原则 1.给$.fn绑定函数,实现插件的代码逻辑 2.插件函数最后要return this,以支持链式调用 3.插件函数要有默认值,绑定在$.fn.<pluginName& ...
- Android日历开发之右上角标注红点事件
1.效果如下所示: 2.方法: 前提:已经知道如何在右上角画圆点的情况下. 这是一个任务显示器,每个任务都有一个时间,比如2014.01.12. 如果要标注2016.08 ...
- 【Educationcal Codeforces Round 21】
这场edu我原本以为能清真一点…… 后来发现不仅是七题 还有各种奇奇怪怪的骚操作…… A. 随便枚举 #include<bits/stdc++.h> using namespace std ...
- C基础 redis缓存访问
引言 先说redis安装, 这里采用的环境是. Linux version --generic (buildd@lgw01-) (gcc version (Ubuntu -14ubuntu2) ) # ...