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++实现)的更多相关文章

  1. 【LeetCode】167. Two Sum II - Input array is sorted

    Difficulty:easy  More:[目录]LeetCode Java实现 Description Given an array of integers that is already sor ...

  2. 【LeetCode】167. Two Sum II - Input array is sorted 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

  3. 167. Two Sum II - Input array is sorted - LeetCode

    Question 167. Two Sum II - Input array is sorted Solution 题目大意:和Two Sum一样,这里给出的数组是有序的 思路:target - nu ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. STM8的AIR与STM32的Keil的指定地址存数据

    [经验分享]KE02在IAR和KEIL中以常量形式初始化EEPROM值一, 经验分享描述        写这篇经验分享的原因是,之前有一个客户,他希望在KE02的芯片中,不要出现使用EEPROM操作命 ...

  2. 【JVM进阶之路】十三:类加载过程

    通过前面的学习,我们了解了Class文件的结构,在Class文件中描述的各类信息,最终都需要加载到虚拟机中之后才能被运行和使用. 接下来,我们开始学习JVM的类加载. 一个类从被加载到虚拟机内存中开始 ...

  3. .Net Core gRPC 实战(二)

    概述 gRPC 客户端必须使用与服务相同的连接级别安全性.  如调用服务时通道和服务的连接级别安全性不一致,gRPC 客户端就会抛出错误. gRPC 配置使用HTTP gRPC 客户端传输层安全性 ( ...

  4. ZooKeeper学习笔记三:使用ZooKeeper实现一个简单的配置中心

    作者:Grey 原文地址:ZooKeeper学习笔记三:使用ZooKeeper实现一个简单的配置中心 前置知识 完成ZooKeeper集群搭建以及熟悉ZooKeeperAPI基本使用 需求 很多程序往 ...

  5. 摄像头 ISP 调试的入门之谈(经验总结)

    在讲述本文之前,我尽量以一个什么也不清楚的初学到入门的用词来阐述什么是 ISP 调试,以及为什么需要调试. 如果你从来都没有接触过什么是摄像头 ISP 调试,我想这个文章可以给你一些启发和关键词. 因 ...

  6. Gamma矫正技术

    Gamma矫正技术 一. gamma校正背景 在电视和图形监视器中,显像管发生的电子束及其生成的图像亮度并不是随显像管的输入电压线性变化,电子流与输入电压相比是按照指数曲线变化的,输入电压的指数要大于 ...

  7. PointRCNN: 点云的3D目标生成与检测

    PointRCNN: 点云的3D目标生成与检测 PointRCNN: 3D Object Proposal Generation and Detection from Point Cloud 论文地址 ...

  8. 中继TensorRT集成

    中继TensorRT集成 介绍 NVIDIA TensorRT是用于优化深度学习推理的库.这种集成将使尽可能多的算子从Relay转移到TensorRT,从而无需调整调度,即可在NVIDIA GPU上提 ...

  9. TensorFlow中的语义分割套件

    TensorFlow中的语义分割套件 描述 该存储库用作语义细分套件.目标是轻松实现,训练和测试新的语义细分模型!完成以下内容: 训练和测试方式 资料扩充 几种最先进的模型.轻松随插即用 能够使用任何 ...

  10. BlazorCharts 原生图表库的建设历程

    作者:陈超超 Ant Design Blazor 项目贡献者,拥有十多年从业经验,长期基于.Net技术栈进行架构与开发产品的工作,现就职于正泰集团. 邮箱:timchen@live.com 欢迎各位读 ...