题意:对于一个有序数组,输出和为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(双指针)的更多相关文章

  1. 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], ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)

    Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted) 给定一个已按照升序排列 的有序数组,找到两个数使得它们 ...

  6. 【LEETCODE】38、167题,Two Sum II - Input array is sorted

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

  7. 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 ...

  8. 167. Two Sum II - Input array is sorted - LeetCode

    Question 167. Two Sum II - Input array is sorted Solution 题目大意:和Two Sum一样,这里给出的数组是有序的 思路:target - nu ...

  9. [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 ...

随机推荐

  1. git相关项目迁移

    1). 从原地址克隆一份裸版本库,比如原本托管于 GitHub. git clone --bare git://github.com/username/project_old.git --bare 创 ...

  2. MVC简要介绍

    (转自:http://www.cnbeta.com/articles/107924.htm) MVC不是一种设计模式(design pattern),它是一种架构模式(Architectural pa ...

  3. Java日期时间API系列13-----Jdk8中java.time包中的新的日期时间API类,时间类转换,Date转LocalDateTime,LocalDateTime转Date等

    从前面的系列博客中可以看出Jdk8中java.time包中的新的日期时间API类设计的很好,但Date由于使用仍非常广泛,这就涉及到Date转LocalDateTime,LocalDateTime转D ...

  4. 01-Spring的概述

    Spring概述 ①Spring是一个开源框架 ②Spring为简化企业级开发而生,使用Spring开发可以将Bean对象,Dao组件对象,Service组件对象等交给Spring容器来管理,这样使得 ...

  5. LeetCode 编辑距离(DP)

    题目 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可以对一个单词进行如下三种操作: 插入一个字符 删除一个字符 替换一个字符 思路 定 ...

  6. PHP中PHP $_POST和PHP $_REQUEST及PHP $_GET的用法及区别

     笔者最近开始学习PHP语言大法,记录一下学习过程中遇到的知识点.          今天介绍的是php中有关 php $_post 和 php $_request 及 php $_get 的用法及区 ...

  7. vscode git连接github

    上一篇文章中介绍了vscode中git的简单使用方法vscode git的简单使用 上次只讲到了本地库的创建,这次说明下怎么push到github上 首先需要有一个github的账号  github官 ...

  8. Java数字和字符的对照关系表

    /* 数字和字符的对照关系表(编码表): ASCII码表:American Standard Code for Information Interchange,美国信息交换标准代码. Unicode码 ...

  9. sort的使用

    sort主要是用来排序的,可以用自定义的函数进行比较,也可以用系统的4中函数进行比较,即less(),greater(),less_equal(),greater_equal().但是我试了一下,发现 ...

  10. mysql修改字符集为utf8

    https://zhidao.baidu.com/question/1642165712897935220.html