要求

  • 升序数组
  • 找到两个数使得它们相加之和等于目标数
  • 函数返回两个下标值(下标从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. 单链表c语言实现的形式

    包括初始化,创建,查询,长度,删除,清空,销毁等操作 代码如下: #include<stdio.h> #include<stdlib.h> //定义单链表的数据类型 typed ...

  2. PAT (Basic Level) Practice (中文)1070 结绳 (25 分) 凌宸1642

    PAT (Basic Level) Practice (中文)1070 结绳 (25 分) 凌宸1642 题目描述 给定一段一段的绳子,你需要把它们串成一条绳.每次串连的时候,是把两段绳子对折,再如下 ...

  3. Sentinel全局Feign默认熔断设计实现

    写在前面 用XMind画了一张导图记录Spring Cloud Alibaba的学习笔记(源文件对部分节点有详细备注和参考资料,由于太大就没展示全部,欢迎关注我的公众号:阿风的架构笔记 后台发送[导图 ...

  4. Redis初学

    1. redis     1. 概念     2. 下载安装     3. 命令操作         1. 数据结构     4. 持久化操作     5. 使用Java客户端操作redis Redi ...

  5. 虚拟机装好centos7没网解决办法

    输入ip查询命名 ip addr  也可以输入 ifconfig(centOs7没有ifconfig命令)查看ip,但此命令会出现3个条目,centos的ip地址是ens33条目中的inet值. 发现 ...

  6. 【笔记】《Redis设计与实现》chapter20 Lua脚本

    chapter20 Lua脚本 Redis从2.6版本开始引入对Lua脚本的支持,通过在服务器中嵌入Lua环境,Redis客户端可以使用Lua脚本,直接在服务器端原子地执行多个Redis命令 20.1 ...

  7. spring boot最新版使用几个坑解决

    <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent ...

  8. Linux 服务管理的两种方式service和systemctl

    service命令 service命令其实是去/etc/init.d目录下,去执行相关程序 ``` # service命令启动redis脚本 service redis start # 直接启动red ...

  9. hdu 5059 判断数字表示方式以及范围合法(int型之内)

    题意:       给你一个串和两个整数a,b,问你这个串表示的数字是否合法,并且在a,b之间, 和法的要求是无论是正数还是负数都没有前导0,并且注意 -0 是不合法的. 思路:       写了将近 ...

  10. Linux文件共享服务之Vsftp

    目录 FTP Vsftp服务的搭建 ftp.sftp.vsftp.vsftpd的区别 FTP FTP的工作原理: FTP会话时包含了两个通道,一个叫控制通道,一个叫数据通道.控制通道是和FTP服务器进 ...