要求

  • 升序数组
  • 找到两个数使得它们相加之和等于目标数
  • 函数返回两个下标值(下标从1开始)

示例

  • 输入:numbers = [2, 7, 11, 15], target = 9
  • 输出:[1,2]

思路

  • 双重遍历(n2)
  • 遍历+二分搜索(nlogn)
  • 遍历,对撞指针(n)
    • 利用升序数组
    • 头尾两个索引
    • 相加大于目标值,移动尾索引
    • 相加小于目标值,移动头索引

代码

 1 class Solution{
2 public:
3 vector<int> twoSum(vector<int>& numbers, int target){
4
5 int l = 0, r = numbers.size()-1;
6 while( l < r ){
7
8 if(numbers[l]+numbers[r]==target){
9 int res[2] = {l+1, r+1};
10 return vector<int>(res,res+2);
11 }
12 else if(numbers[l]+numbers[r]<target)
13 l++;
14 else
15 r--;
16 }
17 throw invalid_argument("no solution.");
18 }
19 };

相关

  • 125 Valid Palindrome
  • 344 Reverse String
  • 345 Reverse Vowels of a String
  • 11 Container With Most Water

[刷题] 167 Two Sum II的更多相关文章

  1. 【LEETCODE】38、167题,Two Sum II - Input array is sorted

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

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

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

  4. leetcode 167 two sum II

    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. 167. Two Sum II - Input array is sorted - LeetCode

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

  8. [LeetCode] #167# Two Sum II : 数组/二分查找/双指针

    一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...

  9. 1. Two Sum + 15. 3 Sum + 16. 3 Sum Closest + 18. 4Sum + 167. Two Sum II - Input array is sorted + 454. 4Sum II + 653. Two Sum IV - Input is a BST

    ▶ 问题:给定一个数组 nums 及一个目标值 target,求数组中是否存在 n 项的和恰好等于目标值 ▶ 第 1题,n = 2,要求返回解 ● 代码,160 ms,穷举法,时间复杂度 O(n2), ...

随机推荐

  1. 自动获取IMC系统所有网络设备资产信息

    1 #coding=utf8 2 3 """ 4 CMDB接口调用 5 """ 6 import csv 7 import json 8 i ...

  2. 前端 | JS 任务和微任务:promise 的回调和 setTimeout 的回调到底谁先执行?

    首先提一个小问题:运行下面这段 JS 代码后控制台的输出是什么? console.log("script start"); setTimeout(function () { con ...

  3. Vue3 封装第三方组件(一)做一个合格的传声筒

    各种UI库的功能都是非常强大的,尤其对于我这种不会 css 的人来说,就更是帮了大忙了. 只是嘛,如果再封装一下的话,那么用起来就会更方便了. 那么如何封装呢? 封装三要素 -- 属性.插槽.事件.方 ...

  4. linux 在某个路径下,查找某个文件

    find /cephfs/netdisk/ -name "*.sql"

  5. 敏捷史话(十五):我发明了敏捷估算扑克牌 —— James Greening

    雪鸟会议 雪鸟会议前夕,James Grenning 在 Object Mentor 与 Robert C. Martin 一同工作,彼时组织雪鸟会议的 Bob 大叔盛情邀请 James,告知他会议的 ...

  6. Day01_05_Java第一个程序 HelloWorld - java类规则

    第一个程序Hello World *基础语法规则: 1. 第一个Java程序 HelloWorld! public class HelloWorld{ public static void main( ...

  7. Day16_92_Java IO 基础概念

    Java IO 基础概念 流根据方向分为 输入流 和 输出流. 注意 : 输入和输出是相对与内存而言的,从内存出来就是输出,到内存中去就是输入. 输入叫做 "读" , 输出叫做 & ...

  8. Day07_33_链表

    链表 单链表 双向链表 * 什么是双向链表? 双向链表是链表的一种,由节点组成,每个数据结点中都有两个指针,分别指向直接后继和直接前驱. ![](https://img2020.cnblogs.com ...

  9. 锁定项目的 node 版本

    一些老项目对 node 版本是有要求的,往往使用默认的新版本包安装不上,scripts 也跑不起来. 之前就遇到过运行一个小程序项目时,根据文档来,第一步安装就出错.本着办法总比问题多的理念,来一个解 ...

  10. Android进程的so注入--Poison(稳定注入版)

    本文博客地址:http://blog.csdn.net/qq1084283172/article/details/53869796 Android进程的so注入已经是老技术了,网上能用的Android ...