LeetCode167. Two Sum II - Input array is sorted(双指针)
题意:对于一个有序数组,输出和为target的两个元素的下标。题目保证仅有唯一解。
分析:
法一:二分。枚举第一个元素,二分找另一个元素,时间复杂度O(nlogn),非最优解。
class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
int len = numbers.size();
vector<int> ans;
for(int i = 0; i < len; ++i){
int tmp = target - numbers[i];
int l = i + 1;
int r = len - 1;
bool ok = false;
while(l <= r){
int mid = l + (r - l) / 2;
if(numbers[mid] == tmp){
ok = true;
ans.push_back(i + 1);
ans.push_back(mid + 1);
break;
}
else if(numbers[mid] < tmp){
l = mid + 1;
}
else{
r = mid - 1;
}
}
if(ok){
break;
}
}
return ans;
}
};
法二:双指针,head和tail分别指向数组中头尾元素,若numbers[head]+numbers[tail]>target,则tail--;若numbers[head]+numbers[tail]<target,则head++;直到numbers[head]+numbers[tail]==target。时间复杂度O(n)。
class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
vector<int> ans;
int head = 0;
int tail = numbers.size() - 1;
while(head < tail){
int sum = numbers[head] + numbers[tail];
if(sum == target){
ans.push_back(head + 1);
ans.push_back(tail + 1);
break;
}
else if(sum < target) ++head;
else --tail;
}
return ans;
}
};
LeetCode167. Two Sum II - Input array is sorted(双指针)的更多相关文章
- 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 ...
随机推荐
- linux日常运维工作
Linux的使用环境也日趋成熟,各种开源产品络绎不绝,大有百花齐放的盛景,那么当Linux落地企业,回归工作时,我们还要面对这Linux运维方面的诸多问题,今天我们特意组织一场有关Linux 在企业运 ...
- Spring Boot整合EhCache
本文讲解Spring Boot与EhCache的整合. 1 EhCache简介 EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认CacheProvid ...
- Java进阶学习(2)之对象交互(上)
对象交互 对象交互 对象的识别 时钟小程序 把现实世界用对象去建模,去分解问题规模,最终抽象成对象和对象的模型 例如11:03的小程序,可以抽象成一个显示类,一个类生成两个对象去表示时钟 packag ...
- C# 酒店管理系统知识点
identity (m,n)自增 m开始n每次增加的值 默认(1,1) 列名 数据类型 约束 identity(m,n) 重新设置identity的值 1.语法 dbcc checkident ...
- static静态变量使用@Value注入方式
@Componentpublic class MyConfig { private static String env; public static String getEnv() { return ...
- markdown列表
Markdown 列表 Markdown 支持有序列表和无序列表. 无序列表使用星号(*).加号(+)或是减号(-)作为列表标记: * 第一项 * 第二项 * 第三项 + 第一项 + 第二项 + 第三 ...
- IDEA Tomcat配置 VM Option
-server -XX:PermSize=512M -XX:MaxPermSize=1024m -Dfile.encoding=UTF-8
- 【PAT甲级】1079 Total Sales of Supply Chain (25 分)
题意: 输入一个正整数N(<=1e5),表示共有N个结点,接着输入两个浮点数分别表示商品的进货价和每经过一层会增加的价格百分比.接着输入N行每行包括一个非负整数X,如果X为0则表明该结点为叶子结 ...
- 命令行选项解析函数getopt()
1.定义: int getopt(int argc, char * const argv[], const char *optstring); 2.描述: getopt是用来解析命令行选项参数的,但是 ...
- enviroment linux jdk and git and maven
#java_home export JAVA_HOME=/usr/local/java/jdk1.8.0_211 export JRE_HOME=$JAVA_HOME/jre export CLASS ...