LeetCode167. Two Sum II - Input array is sorted(双指针)
题意:对于一个有序数组,输出和为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(双指针)的更多相关文章
- 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], ...
- 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 ...
- 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 ...
- 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 ...
- Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)
Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted) 给定一个已按照升序排列 的有序数组,找到两个数使得它们 ...
- 【LEETCODE】38、167题,Two Sum II - Input array is sorted
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 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 ...
- 167. Two Sum II - Input array is sorted - LeetCode
Question 167. Two Sum II - Input array is sorted Solution 题目大意:和Two Sum一样,这里给出的数组是有序的 思路:target - nu ...
- [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 ...
随机推荐
- python连接oracle数据库报错"DatabaseError: DPI-1047: 64-bit Oracle Client library cannot be loaded: "解决方案
操作系统,python3.5, oracle_11, 均为64位:plsql 正常连接. 也顺利安装了cx_oracle 6.3,但是python进行连接的时候就会报错"DatabaseEr ...
- Windows Server 2016安装.NET Framework 3.5
1.打开“服务器管理器” 2.点击“添加角色和功能” 3.点击“下一步” 4.点击“下一步” 5.点击“下一步” 6.点击“下一步” 7.勾选“.NET Framework 3.5功能”,点击“下一步 ...
- Springmvc-crud-01错误
错误:无法显示图书列表内容 原因:获取存储域对象中的名字写错了 controller层: 前端页面: 解决方案:前后端的代码要保持一致(名字自己定义),写代码要细心 修改成功后的界面
- 6、Maven仓库
在Maven的术语中,仓库是一个位置(place),例如目录,可以存储所有的工程.jar文件,library jar文件,插件或者任何其他的工程指定的文件 Maven仓库有三种类型 本地(local) ...
- tkinter+pickle+python的一个登录界面设计
1.代码: #导出模块 import tkinter as tk from tkinter import messagebox import pickle #定义登录的窗口.标题.大小和位置 wind ...
- mysql事务管理及spring声明式事务中主动异常抛出使数据库回滚
mysql的引擎常用的有两个,一个MyISAM,另一个是InnoDB,mysql默认的为MyISAM,而InnoDB才是支持事务的.所以一般需要修改下,如何修改就不说了. 事务需要依赖数据库,好久没使 ...
- 组件向外暴露v-model绑定的参数
<template> <div class="search-box"> <i class="icon-search">< ...
- Anniversary party POJ - 2342
题目链接 经典的树形dp,最大独立集,对于每个点就有2个状态,选/不选 设\(dp_{i,0}\)表示不选第i个,\(dp_{i,1}\)表示选第i个,容易得到其状态转移 \(dp_{i,0} = \ ...
- Wireshark 查看指定进程的网络包
Wireshark 查看指定进程的网络包 打开任务管理器,右键筛选列,选中PID(进程标识符): 找到该进程对应的PID,如1200: 在cmd中执行netstat -ano|findstr 1200 ...
- 实现简单ORM案例
ORM框架: • 我们希望设计一个可以实现对象和SQL自动映射的框架,但是整体用法和设计比Hibernate简单.砍掉不必要的功能.• 会穿插使用设计模式• 增加 – 将对象对应成sql语句,执行sq ...