(双指针 二分) 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 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的更多相关文章
- 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 ...
- [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 ...
- 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 ...
- 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 ...
- ✡ 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 ...
- 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 ...
- 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 ...
- 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 ...
- 167. Two Sum II - Input array is sorted - LeetCode
Question 167. Two Sum II - Input array is sorted Solution 题目大意:和Two Sum一样,这里给出的数组是有序的 思路:target - nu ...
随机推荐
- vue element-ui 分页组件封装
<template> <el-pagination @size-change="handleSizeChange" @current-change="h ...
- 深入浅出KNN算法(一) KNN算法原理
一.KNN算法概述 KNN可以说是最简单的分类算法之一,同时,它也是最常用的分类算法之一,注意KNN算法是有监督学习中的分类算法,它看起来和另一个机器学习算法Kmeans有点像(Kmeans是无监督学 ...
- SQLServer之创建不可重复读
创建不可重复读注意事项 语法:set transaction isolation level repeatable read. 指定语句不能读取已由其他事务修改但尚未提交的行,并且指定,其他任何事务都 ...
- asp.net webapi 的Request如何获取参数
public class BaseApiController : ApiController { private HttpRequestBase _request; /// 全局Requests对象 ...
- SQL Server数据库————增删改查
--增删改查--增 insert into 表名(列名) value(值列表) --删 delect from 表名 where 条件 --改 update 表名 set 列名=值1,列名2=值2 w ...
- c/c++ 右值引用,forward关键字
c++ forward关键字 forward的由来:模板函数中的推导类型,作为另一函数的参数时,不管实参是什么类型,作为另一个参数的实参时,都变成了左值.因为C++里规定函数的形参就是左值,不过调用侧 ...
- oracle 关于对时间操作的汇总
-- 对时间的操作 对当前日期增加一个小时: SQL> select sysdate, sysdate+numtodsinterval(1,’hour’) from dual ; 对当前日期增加 ...
- day 24 面向对象之继承及属性查找顺序
组合 组合:自定义类的对象作为另外一个类的属性 class Teacher: def init(self, name, age): self.name = name self.age = age t1 ...
- IdentityServer4实战 - 与API单项目整合
一.前言 我们在实际使用 IdentityServer4 的时候,可能会在使用 IdentityServer4 项目添加一些API,比如 找回密码.用户注册.修改用户资料等,这些API与Identit ...
- git bash 连接github并提交项目工程
借鉴博客:https://www.cnblogs.com/flora5/p/7152556.html https://blog.csdn.net/heng_yan/article/details/79 ...