[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 * ...
随机推荐
- es题目
1.elasticsearch了解多少,说说你们公司es的集群架构,索引数据大小,分片有多少,以及一些调优手段 .2.elasticsearch的倒排索引是什么?3.elasticsearch 索引数 ...
- 使用flask搭建微信公众号:实现签到功能
终于到了实战阶段.用微信公众号实现一个简单的签到功能. 前情提要: 微信公众号token验证失败 使用flask搭建微信公众号:完成token的验证 使用flask搭建微信公众号:接收与回复消息 程序 ...
- ThreadLocal(在一个线程中共享数据)
ThreadLocal 在"事务传递Connection"参数案例中,我们必须传递Connection对象,才可以完成整个事务操作.如果不传递参数,是否可以完成?在JDK中给我们提 ...
- Chirp信号及其生成
Chirp信号是一个典型的非平稳信号,在通信.声纳.雷达等领域具有广泛的应用. 简介 Chirp译名:啁啾(读音:“周纠”),是通信技术有关编码脉冲技术中的一种术语,是指对脉冲进行编码时,其载频在脉冲 ...
- 洛谷 P5506 封锁
目录 题目 思路 \(Code\) 题目 P5506 封锁 思路 模拟 \(\large\text{读题一定要细心}\) 解释都在代码里. \(Code\) #include<bits/stdc ...
- P4279 【[SHOI2008]小约翰的游戏】
我怎么什么都不会啊\(QAQ\)博弈论怎么和期望一样玄学啊\(QAQ\) 我们分几种情况讨论: \(Case1\):只有一堆且为1,那么后手胜利 \(Case2\):每一堆都是1,那么只需要判断奇偶性 ...
- Python之NumPy(axis=0/1/2...)的透彻理解
https://blog.csdn.net/sky_kkk/article/details/79725646 numpy中axis取值的说明首先对numpy中axis取值进行说明:一维数组时axis= ...
- windows环境搭建dubbo服务
windows环境搭建dubbo服务 1:首先需要下载dubbo的注册中心 zookeeper zookeeper注册中心下载地址链接:http://mirror.bit.edu.cn/apache/ ...
- K8S API对象
POD Pod是在K8s集群中运行部署应用或服务的最小单元,它是可以支持多容器的.Pod的设计理念是支持多个容器在一个Pod中共享网络地址和文件系统,可以通过进程间通信和文件共享这种简单高效的方式组合 ...
- [C++] 对象指针使用方法
对象指针:指向类对象的指针 类指针指向类变量(对象)的地址 对象指针定义格式: 类类型 *变量名: 举例: #include <iostream> using namespace std; ...