[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 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.
思路:
与1. Tow Sum类似,这题的输入是有序数组,限定了一定会有解,用双指针来做,定义左右两个指针,左指针指向第一个数,右指针指向最后一个数,然后用这两个数的和与Target比较,如果比Target小,左指针向右移一位,如果比Target大,右指针向左移一位。然后再进行比较,直到找到或者两个指针相遇为止。
注意:左右指针是从0到 len(numbers)-1, 输出结果是从1开始的index.
Time: O(n) Space: O(1)
Java: wo, 0 ms, faster than 100.00% of Java online submissions
class Solution {
public int[] twoSum(int[] numbers, int target) {
int[] res = new int[2];
int i = 0, j = numbers.length - 1;
while (i < j) {
if ((numbers[i] + numbers[j]) > target) {
j--;
} else if ((numbers[i] + numbers[j]) < target) {
i++;
} else {
res[0] = i + 1;
res[1] = j + 1;
break;
}
}
return res;
}
}
Java: 1 ms, faster than 68.07% of Java online submissions
public class Solution {
public int[] twoSum(int[] numbers, int target) {
if(numbers==null || numbers.length < 1) return null;
int i=0, j=numbers.length-1; while(i<j) {
int x = numbers[i] + numbers[j];
if(x<target) {
++i;
} else if(x>target) {
--j;
} else {
return new int[]{i+1, j+1};
}
}
return null;
}
}
Python:
class Solution:
def twoSum(self, nums, target):
start, end = 0, len(nums) - 1 while start != end:
sum = nums[start] + nums[end]
if sum > target:
end -= 1
elif sum < target:
start += 1
else:
return [start + 1, end + 1]
C++:
class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
int l = 0, r = numbers.size() - 1;
while (l < r) {
int sum = numbers[l] + numbers[r];
if (sum == target) return {l + 1, r + 1};
else if (sum < target) ++l;
else --r;
}
return {};
}
};
相似题目:
[LeetCode] 170. Two Sum III - Data structure design 两数之和之三 - 数据结构设计
[LeetCode] 653. Two Sum IV - Input is a BST 两数之和之四 - 输入是二叉搜索树
[LeetCode] 167. Two Sum II - Input array is sorted 两数和 II - 输入是有序的数组的更多相关文章
- 167 Two Sum II - Input array is sorted 两数之和 II - 输入有序数组
给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数.函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2.请注意,返回的下标值(i ...
- [LeetCode] 653. Two Sum IV - Input is a BST 两数之和之四 - 输入是二叉搜索树
Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...
- [LeetCode] 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 ...
- 167. Two Sum II - Input array is sorted两数之和
1. 原始题目 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2. 说明 ...
- [LeetCode] Two Sum IV - Input is a BST 两数之和之四 - 输入是二叉搜索树
Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...
- Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)
Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted) 给定一个已按照升序排列 的有序数组,找到两个数使得它们 ...
- 167. Two Sum II - Input array is sorted - LeetCode
Question 167. Two Sum II - Input array is sorted Solution 题目大意:和Two Sum一样,这里给出的数组是有序的 思路:target - nu ...
- 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 ...
- 【LEETCODE】38、167题,Two Sum II - Input array is sorted
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
随机推荐
- .NET 使用 VLC 播放视频
使用 VLC 播放监控有几个月了,现在是多个项目中都有用到.在使用的过程中也有一些细节供大家参考. 一.对 VLC 的了解 VLC 是一个开源的跨平台多媒体播放器及框架. VLC 官方出的有播放器.编 ...
- 在vue项目中使用自己封装的ajax
在 src 目录下新建 vue.extend.js ,内容如下: export default { install(Vue) { Vue.prototype.$http=function(option ...
- Alpha版本1之后的成绩汇总
作业要求 1.作业内容: 作业具体要求及评分标准的链接 2.评分细则 •给出开头和团队成员列表(10’) •给出发布地址以及安装手册(20’) •给出测试报告(40’) •给出项目情况总结(30’) ...
- 查看mysql执行时间
mysql的 profiling不是默认打开的 查看profiling是否找开 mysql> show variables like "%pro%"; +---------- ...
- docker拷贝宿主与容器中的文件
从容器里面拷文件到宿主机 语法:docker cp 容器名:要拷贝的文件在容器里面的路径 要拷贝到宿主机的相应路径 例子:容器名为ubuntu,要从容器里面拷贝的文件路为:/usr/local/tom ...
- 抓取Dump文件的方法和工具介绍
一.Windows系统的任务管理器里抓dump 启动任务管理器,选中某个进程,右键,弹出菜单"创建转储文件" 注意事项: 当你在64位Windows系统上抓32位进程的dmup文件 ...
- proxysql 学习一 proxysql docker 运行试用
proxysql 是一个比较强大的mysql proxy 服务,支持动态mysql 实例调整,查询重写,查询cache,监控,数据镜像,读写分离 以及ha,最近已经发布了2.0 ,很值得试用下 环境准 ...
- Tomcat启动问题:严重[main] org.apache.catalina.core.AprLifecycleListener.init An incompatible version...
今天观察tomcat启动日志,有一些以前没注意到的信息: 严重 [main] org.apache.catalina.core.AprLifecycleListener.init An incompa ...
- 开源项目 06 NPOI
using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using System; using S ...
- Generating a Random Sample from discrete probability distribution
If is a discrete random variable taking on values , then we can write . Implementation of this formu ...