608. Two Sum - Input array is sorted【medium】
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. Please note that your returned answers (both index1 and index2) are not zero-based.
Notice
You may assume that each input would have exactly one solution.
Given nums = [2, 7, 11, 15], target = 9
return [1, 2]
解法一:
public class Solution {
/*
* @param nums: an array of Integer
* @param target: target = nums[index1] + nums[index2]
* @return: [index1 + 1, index2 + 1] (index1 < index2)
*/
public int[] twoSum(int[] nums, int target) {
int[] res = new int[]{-1, -1};
int left = 0, right = nums.length - 1;
while (left < right) {
if (nums[left] + nums[right] == target) {
res[0] = left + 1;
res[1] = right + 1;
break;
} else if (nums[left] + nums[right] > target) {
right--;
} else {
left++;
}
}
return res;
}
}
典型的双指针思路
608. Two Sum - Input array is sorted【medium】的更多相关文章
- 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【Easy】【双指针-有序数组求两数之和为目标值的下标】
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- 【LEETCODE】38、167题,Two Sum II - Input array is sorted
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 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@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_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 ...
随机推荐
- python中出现 “'gbk' codec can't decode byte 0xf3 in position 20: illegal multibyte sequence”问题
其实是打开文件方法open()中的模式有r,w,a等. 请看: r 以只读方式打开文件.文件的指针将会放在文件的开头.这是默认模式. rb 以二进制格式打开一个文件用于只读.文件指针将会放在文件的开头 ...
- .NET Oracle Developer的福音——ODP.NET Managed正式推出
.NET Oracle Developer的福音--ODP.NET Managed正式推出 在.NET平台下开发Oracle应用的小伙伴们肯定都知道一方面做Oracle开发和实施相比SqlServ ...
- 支持解析GitHub Flavored Markdown(GFM)的PHP库-Parsedown
网上搜索PHP的markdown解析库,只能找得到Michel的PHP Markdown,这个库很不错,但是他只能支持标准markdown和他自己定义的一套扩展php Markdown Extra.这 ...
- Python学习之路上的几个经典问题
1.python有三元运算符语法(类似C语言的"?")么? 语法如下: [on_true] if [expression] else [on_false] 如果[expressio ...
- openwrt web server
刚刚群里又个同学问PHP页面放到哪个文件夹下. 我一想那得apache呀,于是我说你得先看自己的路由器能不能装的下apache. 但是回头他又问,那openwrt的页面怎么弄的? 我一时语塞,于是就百 ...
- 下载论坛源码GBK UTF8 BIG5分别是什么意思
下载论坛源码GBK UTF8 BIG5分别是什么意思? 提问者:ly1987520 | 浏览次数:4010次 下载论坛源码简体中文GBK 简体中文UTF8 繁体中文BIG5 分别是什么意思?他们的区别 ...
- XP系统如何把桌面图标变大
右击桌面,属性,外观,高级,在项目里面找到图标,大小改为你喜欢的样式. 我测试的结果是:图标大小改为42,字体大小改为8,图标垂直间距改为100,水平间距改为54效果不错.
- Uber 四年时间增长近 40 倍,背后架构揭秘
据报道,Uber 仅在过去4年的时间里,业务就激增了 38 倍.Uber 首席系统架构师 Matt Ranney 在一个非常有趣和详细的访谈<可扩展的 Uber 实时市场平台>中告诉我们 ...
- 解决grep的结果无法显示文件名的问题
有时候想在代码中执行某个关键词,会用下面的语句: find . -type f -name "*.java" | xargs grep -n "<keyword&g ...
- CodeIgniter 2.X 于 PHP5.6 兼容错误
本篇文章由:http://xinpure.com/codeigniter-2-x-to-php5-6-compatible-error/ CI 3.0 已兼容此问题 在代码迁移的过程中,遇到了一个 P ...