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. 利用data-src属性 更换图片

    <div class="img_src"> <img src="./images/luo.png"></div> <u ...

  2. 重拾《 两周自制脚本语言 》- Eclipse插件实现语法高亮

    源码库: program-in-chinese/stone-editor-eclipse 参考: FAQ How do I write an editor for my own language? D ...

  3. (最完美)MIUI12系统的Usb调试模式在哪里开启的步骤

    当我们使用安卓手机通过数据线链接到Pc的时候,或者使用的有些app比如我们公司营销小组当使用的app引号精灵,之前的老版本就需要开启usb调试模式下使用,现当新版本不需要了,如果手机没有开启usb调试 ...

  4. STL源码剖析-vector

    STL(Standard Template Library) C++标准模板库,acm选手一定对它不陌生,除了算法,那几乎是“吃饭的家伙了”.我们使用库函数非常方便,且非常高效(相对于自己实现来说). ...

  5. Spark MLlib KMeans 聚类算法

    一.简介 KMeans 算法的基本思想是初始随机给定K个簇中心,按照最邻近原则把分类样本点分到各个簇.然后按平均法重新计算各个簇的质心,从而确定新的簇心.一直迭代,直到簇心的移动距离小于某个给定的值. ...

  6. Docker-通过docker-maven-plugin插件实现docker镜像构建并自动发布到远程docker服务器

    我们知道,docker能实现应用打包隔离,实现快速部署和迁移.如果我们开发应用使用了spring cloud + spring boot架构,那么,通过docker-maven-plugin实现快速构 ...

  7. Docker-Dockerfile及基本语法

    Dockerfile的作用是通过它可以生成自定镜像,先介绍几个基本的docker命令. [docker镜像相关的命令]docker search 镜像名: 搜索镜像docker pull 镜像名: 镜 ...

  8. SQLServer之创建提交读

    事务提交读注意事项 语法:set transaction isolation level read committed. 数据库默认的是两个会话事务之间是提交读. READ COMMITTED指定语句 ...

  9. 网络基础 外网IP,内网IP,虚拟机的网络设置

    外网IP,内网IP的关系 在这三类地址中,绝大多数的IP地址都是公有地址,需要向国际互联网信息中心申请注册.但是在IPv4地址协议中预留了3个IP地址段,作为私有地址,供组织机构内部使用. 这三个地址 ...

  10. linux的自有(内置)服务

    运行模式(运行级别) 在linux中存在一个进程,init(initialize初始化)进程号为1 ,该进程对应一个配置文件inittab 文件路径为/etc/inittab centOS6.5存在7 ...