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++ ) - 解题报告的更多相关文章

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

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

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

  4. (双指针 二分) 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 ...

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

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

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

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

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

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

随机推荐

  1. jdk8新特性之双冒号 :: 用法及详解

    jdk8的新特性有很多,最亮眼的当属函数式编程的语法糖,本文主要讲解下双冒号::的用法. 概念 类名::方法名,相当于对这个方法闭包的引用,类似js中的一个function.比如: Function& ...

  2. es6 Set 和Map 数据结构

    ES6提供了新的数据结构Set,它类似于数组,但是成员的值都是唯一的,没有重复的值. Set 本身是一个数据结构,用来生成Set 数据结构. const s = new Set(); [2,3,5,4 ...

  3. Nodejs中获取参数以及处理参数

    先看题干效果 在这里我们建了一个表单 填入表单需要提交的信息 对两个参数进行获取和一个加法计算 表单html代码 <form action='http://localhost:8080' met ...

  4. 【模板】BM算法(找线性规律万能模板)

    (1) n是指要找该数列的第n项. (2) 往vec中放入该数列前几项的值,越多越精确. #include<set> #include<cmath> #include<v ...

  5. Mac 字典扩充包 包括 app

    https://pan.baidu.com/s/1htKUaiWZFZJGO6w9azsbsg

  6. ORA-00911: 无效字符 问题和解决

    1.原本java插入数据库表数据写法是这样的 String sql = "INSERT INTO AAA (id1,id2,id3,id4) VALUES ('1','2','3','4') ...

  7. 「PHP」工厂方法模式

    引言   所属:创建型模式,常用设计模式之一 工厂模式分为:简单工厂模式.工厂方法模式.静态工厂模式.抽象工厂模式. 下面为工厂方法模式. 参考资料: <大话设计模式>程杰   模式概述 ...

  8. Hive(9)-自定义函数

    一. 自定义函数分类 当Hive提供的内置函数无法满足你的业务处理需要时,此时就可以考虑使用用户自定义函数. 根据用户自定义函数类别分为以下三种: 1. UDF(User-Defined-Functi ...

  9. Java实例 Part2:Java语言基础

    Part2:Java语言基础 ** Example01:从控制台接收输入字符 ** 运行结果: 实现代码: import java.util.Scanner; public class Example ...

  10. python在lxml中使用XPath语法进行#数据解析

    在lxml中使用XPath语法: 获取所有li标签: from lxml import etree html = etree.parse('hello.html') print type(html) ...