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)

由于数组是已经排序好的,所以用二分查找,时间复杂度为O(nlogn)。就是先遍历数组,然后查找这个数的右边的是否有一个数,这个数与它相加得到目标数。

C++代码:

class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
for(int i = ;i < numbers.size(); i++){
int t = target - numbers[i],left = i + ,right = numbers.size() - ;
while(left <= right){
int mid = left + (right - left)/;
if(numbers[mid] == t) return {i+,mid+};
else if(numbers[mid] < t) left = mid + ;
else right = mid - ;
}
}
return {};
}
};

2)

不过二分查找的时间复杂度比较大,可以用双指针,时间复杂度为线性。空间复杂度为O(1)。

C++代码:

class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
int l = ,r = numbers.size() - ;
while(l < r){
if(numbers[l] + numbers[r] == target) return {l+,r+};
else if(numbers[l] + numbers[r] > target) r--;
else l++;
}
return {};
}
};

(双指针 二分) leetcode 167. Two Sum II - Input array is sorted的更多相关文章

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

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

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

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

    Problem: Given an array of integers that is already sorted in ascending order, find two numbers such ...

  5. ✡ leetcode 167. Two Sum II - Input array is sorted 求两数相加等于一个数的位置 --------- java

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

  6. Java [Leetcode 167]Two Sum II - Input array is sorted

    题目描述: Given an array of integers that is already sorted in ascending order, find two numbers such th ...

  7. LeetCode - 167. Two Sum II - Input array is sorted - O(n) - ( C++ ) - 解题报告

    1.题目大意 Given an array of integers that is already sorted in ascending order, find two numbers such t ...

  8. LeetCode 167. 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 ...

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

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

随机推荐

  1. python的学习笔记__初识函数

    函数定义与调用 #函数定义 def mylen(): """计算s1的长度""" s1 = "hello world" ...

  2. 智能指针std::weak_ptr

    std::weak_ptr 避免shared_ptr内存泄漏的利器.

  3. 使用GDB调试Android Native 层代码

    --------------步骤:0. adb root0. adb shell0. ps | grep browser1. gdbserver :5039 --attach pid2. adb fo ...

  4. 什么是Docker,它可干什么?

    定义我们知道,软件依赖的环境大致包括: 1• 配置文件2• 代码3• tomcat4• JDK5• 操作系统 Docker作为一个软件集装箱化平台,可以让开发者构建应用程序时,将它与其依赖环境一起打包 ...

  5. Postgres 优雅存储树形数据

    碰到一个树形数据需要存储再数据控制,碰到以下两个问题: 在PG数据库中如何表达树形数据 如何有效率的查询以任意节点为Root的子树 测试数据 为了更加简单一些,我们将使用一下数据 Section A ...

  6. windows10滑轮bug

    今天我突然发现我一点也忍受不了在UWP应用.wi10窗口.设置等界面无法使用鼠标滑轮了.这个bug已经困扰了我差不多一年了,从买了这台笔记本就开始了.而且这个问题在中间的某一次升级系统后,也修复了,但 ...

  7. 数据库【mongodb篇】基本命令学习笔记

    MongoDB基本命令用 MongoDB基本命令用   成功启动MongoDB后,再打开一个命令行窗口输入mongo,就可以进行数据库的一些操作. 输入help可以看到基本操作命令: show dbs ...

  8. Java异步NIO框架Netty实现高性能高并发

    原文地址:http://blog.csdn.net/opengl_es/article/details/40979371?utm_source=tuicool&utm_medium=refer ...

  9. jeecg入门操作—表单界面

    一.搭建jeecg开发环境 参考环境搭建步骤 https://www.cnblogs.com/dyh004/p/10687633.html 二.创建用户数据库表: 登录上jeecg平台,点击在线开发- ...

  10. 记一次CPU飙升BUG

    图文地址:https://mp.weixin.qq.com/s?__biz=Mzg3NjEzODQ4NQ==&mid=2247483690&idx=1&sn=7c926f400 ...