Leetcode No.167 Two Sum II - Input array is sorted(c++实现)
1. 题目
1.1 英文题目
Given an array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number.
Return the indices of the two numbers (1-indexed) as an integer array answer of size 2, where 1 <= answer[0] < answer[1] <= numbers.length.
The tests are generated such that there is exactly one solution. You may not use the same element twice.
1.2 中文题目
一个有序数组,找到两个数和等于特定数的位置。
注意:索引从1开始并且数组中的一个元素只能用一次。
1.3输入输出
| 输入 | 输出 |
|---|---|
| numbers = [2,7,11,15], target = 9 | [1,2] |
| numbers = [2,3,4], target = 6 | [1,3] |
| numbers = [-1,0], target = -1 | [1,2] |
1.4 约束条件
- 2 <= numbers.length <= 3 * 104
- -1000 <= numbers[i] <= 1000
- numbers is sorted in non-decreasing order.
- -1000 <= target <= 1000
- The tests are generated such that there is exactly one solution.
2. 分析
2.1 暴力求解法
这一题首先可以利用暴力求解法,遍历所有可能的组合,复杂度为O(\(n^{2}\)),代码如下:
class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
vector<int> result(2);
for (int i = 0; i < numbers.size() - 1; i++)
{
for (int j = i + 1; j < numbers.size(); j++)
{
if (target - numbers[i] == numbers[j])
{
result = { ++i, ++j };
break;
}
}
}
return result;
}
};
这种方法运行效率太低,而且没有利用数组有序的条件
2.2 哈希表法
这种方法是参考leetcode第一题的解法,第一题和该题唯一的差异是第一题数组无序,因此第一题的哈希表法同样适用于本题,但是没有利用到数组有序的条件,非最优,时间复杂度为O(n)。代码如下:
class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
// 哈希表
map<int, int> hashMap;
vector<int> ans;
int temp = 0;
for (int i = 0; i < numbers.size(); i++)
{
temp = target - numbers[i];
if (hashMap.count(temp))
{
ans = { ++hashMap[temp], ++i };
break;
}
hashMap[numbers[i]] = i;
}
return ans;
}
};
2.3 二分法
遍历一个,查找另一个,而数组又是有序的,很容易想到二分法,时间复杂度为O(\(nlogn\))。具体代码如下:
class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
//二分法
vector<int> result;
for (int i = 0; i < numbers.size() - 1; i++)
{
int second = target - numbers[i];
int left = i + 1;
int right = numbers.size() - 1;
while (left <= right)
{
int mid = (left + right) / 2;
if (second < numbers[mid])
right = mid - 1;
else if (second > numbers[mid])
left = mid + 1;
else
{
result.push_back(++i);
result.push_back(++mid);
break;
}
if (result.size() == 2)
break;
}
}
return result;
}
};
2.4 头尾指针法
该方法特别秒,利用两个指针分别指向头尾,通过头尾数之和和目标数进行比较,前者大则尾指针左移,前者小则指针右移。充分利用了排好序数组这一特性,时间复杂度为O(n),代码如下:
public:
vector<int> twoSum(vector<int>& numbers, int target) {
vector<int> result;
int head = 0;
int tail = numbers.size() - 1;
while (head < tail)
{
int tempAdd = numbers[head] + numbers[tail];
if (tempAdd < target)
head++;
else if (tempAdd > target)
tail--;
else
{
result = { ++head, ++tail };
break;
}
}
return result;
}
};
参考:https://www.jianshu.com/p/f3a8a247f4c8
Leetcode No.167 Two Sum II - Input array is sorted(c++实现)的更多相关文章
- 【LeetCode】167. Two Sum II - Input array is sorted
Difficulty:easy More:[目录]LeetCode Java实现 Description Given an array of integers that is already sor ...
- 【LeetCode】167. Two Sum II - Input array is sorted 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 167. Two Sum II - Input array is sorted - LeetCode
Question 167. Two Sum II - Input array is sorted Solution 题目大意:和Two Sum一样,这里给出的数组是有序的 思路:target - nu ...
- 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 1.Two Sum 、167. Two Sum II - Input array is sorted 、15. 3Sum 、16. 3Sum Closest 、 18. 4Sum 、653. Two Sum IV - Input is a BST
1.two sum 用hash来存储数值和对应的位置索引,通过target-当前值来获得需要的值,然后再hash中寻找 错误代码1: Input:[3,2,4]6Output:[0,0]Expecte ...
- [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 ...
随机推荐
- CSS的起步
初学CSS CSS语法规范 选择器{样式} 给谁改样式{改什么样式} 写在style标签里 健值对形式,分号结尾 color:red; <style> p { color:red; fon ...
- 基于 element-plus 封装一个依赖 json 动态渲染的查询控件
前情回顾 基于 el-form 封装一个依赖 json 动态渲染的表单控件 Vue3 封装第三方组件(一)做一个合格的传声筒 功能 使用 vue3 + element-plus 封装了一个查询控件,专 ...
- 详细教程丨如何利用Rancher和Kong实现服务网格?
服务网格(Service mesh)是当前新兴的架构模式,越来越受到人们的青睐.与Kubernetes一起,服务网格可以形成一个强大的平台,它可以解决在微服务集群或服务基础设施上发现的高度分布式环境中 ...
- 004:ZYNQ_AXI总线学习笔记(1)
1. WHAT IS AXI? AXI是一种高级可扩展接口,是ARM AMBA的一部分. 2. WHAT IS AMBA? AMBA是高级微控制器总线架构,开放的片内互联总线标准. 3.A ...
- Python+selenium 自动化-启用带插件的chrome浏览器,调用浏览器带插件,浏览器加载配置信息。
Python+selenium 自动化-启用带插件的chrome浏览器,调用浏览器带插件,浏览器加载配置信息. 本文链接:https://blog.csdn.net/qq_38161040/art ...
- Python+Selenium - 三种等待方式
元素:存在 > 可见 > 可用 需要判断元素状态 等待方式1:强制等待 -- 辅助 设置等待几秒,就必须等待几秒 示例: from time import sleepsleep(3) 强 ...
- 固态LiDAR,半固态混合LiDAR,机械LiDAR
固态LiDAR,半固态混合LiDAR,机械LiDAR 1. APD/SPAD 2轴MEMS扫描镜+ SPAD图像传感器在混合固态LiDAR中的应用 APD的工作模式分为线性模式和盖革模式两种.当APD ...
- Camera Lens Coating
Camera Lens Coating Coating Progress 转换镜头,根据要求进行OEM和设计. 光学元件:望远镜.显微镜.相机和数码相机镜头.放大镜头和远摄镜头.定心镜头.投影镜头.投 ...
- 用 Flutter 和 Firebase 轻松构建 Web 应用
作者 / Very Good Ventures Team 我们 (Very Good Ventures 团队) 与 Google 合作,在今年的 Google I/O 大会上推出了 照相亭互动体验 ( ...
- 这 7 个 Linux 命令,你是怎么来使用的?
使用 Linux 系统的开发者,很多人都有自己喜欢的系统命令,下面这个几个命令令是我平常用的比较多的,分享一下. 我不会教科书般的罗列每个指令的详细用法,只是把日常开发过程中的一些场景下,经常使用的命 ...