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 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. Please note that your returned answers (both index1 and index2) are not zero-based.
You may assume that each input would have exactly one solution.
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
给定一个已排序的整数数组numbers,找出其中两个相加能够等于target的数字。输出它们在数组里的序号。
2.思路
看了一下这道题的其它解题报告,有的写得太复杂了。普遍的第一个思路是两个for循环,这个思路是不大好的,时间复杂性是$O(n^2)$,数组稍微大一点的时候就会超时。第二个比较普遍思路是找target/2,以此为分界线,往前和往后判断,但这个方法其实也不是最好的。第三个思路就是接下来要说的,复杂性为$O(n)$的思路——从两端开始,分别记为i和j:
(1)如果numbers[i]+numbers[j]的结果大于target的话就把j缩小,因为如果再把i增加,那么numbers[i]+numbers[j]结果就更大了。
(2)如果numbers[i]+numbers[j]的结果小于target的话就把i增大,因为如果再把j减小,那么numbers[i]+numbers[j]结果就更小了。
3.代码
class Solution
{
public:
vector<int> twoSum(vector<int>& numbers, int target)
{
int i=0,j=numbers.size()-1;
while(i<j)
{
if(numbers[i]+numbers[j]==target)
return {i+1,j+1};
else if(numbers[i]+numbers[j]>target)
j--;
else if(numbers[i]+numbers[j]<target)
i++;
}
return {0,0};
}
};
LeetCode - 167. Two Sum II - Input array is sorted - O(n) - ( C++ ) - 解题报告的更多相关文章
- 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
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
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 ...
随机推荐
- angular常用属性大全
Angular元素属性大全 addClass()-为每个匹配的元素添加指定的样式类名 after()-在匹配元素集合中的每个元素后面插入参数所指定的内容,作为其兄弟节点 append()-在每个匹配元 ...
- java 整型数据转换为小数类型 BigDecimal 装换为Double
A,B为String类型 ,A-B=C BigDecimal A=(BigDecimal) map.get("A"); BigDecimal B=(BigDecimal) map. ...
- iOS 12 真机调试 Xcode 9 提示 Could not locate device support files.
升级 iOS 12 之后,使用 Xcode 9 真机调试会提示错误: Could not locate device support files. This iPhone 6 Plus is runn ...
- layui sleect获取value值
<div class="layui-form-item"> <label for="username" class="layui-f ...
- AtCoder Grant Contest 10.F 博弈
题意:给定一棵树,每个节点有个权值,Alice和Bob轮流进行操作,给定游戏起点指针指向节点C.不断进行下述操作. 1.将当前节点权值-1,然后将指针从该节点移动到相邻节点,出现一方不能移动(即指针指 ...
- C指针(3)——指向指针的指针(程序讲解)
int **q可以分成两部分,即int* 和 (*q),后面的 “q” 中的* 表示q是一个指针变量,前面的int*表示指针变量q只能存放int*型变量的地址.int** q表示为指针变量q只能存放i ...
- Go语言中的流程控制
1 概述 Go语言提供了条件分支 if,状态分支 switch,循环 for,跳转 goto,延迟执行 defer,这些流程控制语句.分别作说明如下: 2 条件分支 if 依据条件是否满足确定执行哪个 ...
- MongoDB入门---文档查询之$type操作符&limit方法&skip方法&简单排序(sort)操作
上一篇文章呢,已经分享过了一部分查询操作了,这篇文章呢?就来继续分享哈.接下来呢我们直接看MongoDB中的$type操作符哈.它呢是基于BSON类型来检索集合中匹配的数据类型,并且返回结果,在Mon ...
- 博科Brocade 300光纤交换机配置zone教程
光纤交换机作为SAN网络的重要组成部分,在日常应用中非常普遍,本次将以常用的博科交换机介绍基本的配置方法. 博科300实物图: 环境描述: 如上图,四台服务器通过各自的双HBA卡连接至两台博科300光 ...
- TensorFlow之tf.nn.dropout():防止模型训练过程中的过拟合问题
一:适用范围: tf.nn.dropout是TensorFlow里面为了防止或减轻过拟合而使用的函数,它一般用在全连接层 二:原理: dropout就是在不同的训练过程中随机扔掉一部分神经元.也就是让 ...