(双指针 二分) 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 ...
随机推荐
- Dynamics CRM项目实例之七:站点地图修改,联系人-订单-积分管理
关注本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复138或者20141229可方便获取本文,同时可以在第一时间得到我发布的最新的博文信息,follow me! 前面文章发表后,不 ...
- Chromium被用于Microsoft Edge与ChakraCore的未来【译】
注:英语不好,力求大概能懂.持笔人是:Limin Zhu,好像是中国人,但是没有提供中文版本. 大家好,ChakraCore的朋友们: 昨天,微软公布,Microsoft Edge桌面浏览器采用Chr ...
- row_number() over()分页查询
1.首先讲下row_number() over() 是干什么的? 是一个分析函数,会在数据表生成一个排序列. 案例SQL: select ROW_NUMBER() over(order by book ...
- python函数与函数式编程
https://www.cnblogs.com/evablogs/p/6699515.html 在理解函数式编程之前,我还是对函数的调用,参数传递以及函数的嵌套调用一头雾水,还是花了点时间整理了写思绪 ...
- Python基础之注释,算数运算符,变量,输入和格式化输出
Python的注释 注释的作用:用自己熟悉的语言,对某些代码进行标注说明,增强程序的可读性: 在python解释器解释代码的过程中,凡是#右边的,解释器都直接跳过这一行: 注释的分类 单行注释 # 这 ...
- [已解决]报错:Required request body is missing
问题代码: res = requests.post(getXxxxList_url, headers=headers, data={}) 对象网站: angular4 apache 通过验证 (coo ...
- Pycharm 常用快捷键
常用快捷键 快捷键 功能 Ctrl + Q 快速查看文档 Ctrl + F1 显示错误描述或警告信息 Ctrl + / 行注释(可选中多行) Ctrl + Alt + L 代码格式化 Ctrl + A ...
- 基于nodejs的流水线式的CRUD服务。依赖注入可以支持插件。
写代码好多年了,发现大家的思路都是写代码.写代码.写代码,还弄了个称号——码农. 我是挺无语的,我的思路是——不写代码.不写代码.不写代码! 无聊的代码为啥要重复写呢?甚至一写写好几年. 举个例子吧, ...
- 本地跑 spark ui 报错
java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.isAsyncStarted()Z at org.spark_pr ...
- jeecg入门操作—菜单管理
一.菜单配置入口 登录jeecg平台,点击系统管理->菜单管理,弹出菜单管理界面 二.配置一级菜单 点击菜单录入 三.配置二级菜单 选中生成的一级菜单,点击菜单录入 四.菜单授权 五.注销系 ...