题意:对于一个有序数组,输出和为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. Python学习之Craps赌博游戏篇

    在此先安利一波大佬的Python学习项目地址:https://github.com/jackfrued/Python-100-Days 这些天一直在看着大佬的项目学习Python,这是第五天循环学习完 ...

  2. 公告 & 留言板 & 随想录

    欢迎dalao在评论区留言 \(Q \omega Q\) 公告部分: 博客文章的更新一般被放在周末 当然还是可能会咕 自从改了博客的主题之后,文章中的引用好像都会显示出一堆乱码. 由于之前写过的博文不 ...

  3. 集合转换为数组toArray(),数组转换为集合asList()

    package seday12; import java.util.ArrayList;import java.util.Arrays;import java.util.Collection; /** ...

  4. Integer数值小于127时使用==比较的坑

    Java在处理Integer时使用了一个缓存,其中缓存了-128到127之间的数字对应的Integer对象,所以在绝大多数情况下,当我们把2个小于128的Integer对象用==比较时,Java往往是 ...

  5. 安卓之滚动视图ScrollView

    (1)垂直方向滚动时,layout_width要设置为match_parent,layout_height要设置为wrap_content (2)水平方向滚动时,layout_width要设置为wra ...

  6. CSS实现心形、六角星、六边形、平行四边形等几何

    本文将利用border属性实现简单几何的绘制: 效果图: 正八角星 说明:采用两个正方形以中心进行旋转叠加: /* 八角星 */ #burst-8 { background: #6376ff1f; w ...

  7. JS中的提升(即变量和函数声明移动到代码顶部)

    先看代码(第一个代码片段): console.log(a); var a = 1; 如果你认为这是一段不合法的代码,在调用console.log()的时候会输出undefined,你完全正确.但是如果 ...

  8. JS-find、filter、forEach、map

    js这四个方法不会对空数组进行检测,也不会改变原始数组 find()方法主要用来返回数组中符合条件的第一个元素(没有的话,返回undefined) //语法 array.find(function(v ...

  9. 红黑树java代码实现

    红黑树 思想源于:https://www.cnblogs.com/nananana/p/10434549.html有解释有图,很清晰(删除时需考虑根节点和兄弟节点的子节点是否存在) package t ...

  10. Thymeleaf Tutorial 文档 中文翻译

    原文档地址:https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html Thymeleaf官网地址:https://www.thym ...