要求

  • 升序数组
  • 找到两个数使得它们相加之和等于目标数
  • 函数返回两个下标值(下标从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. 人多力量大vs.两个披萨原则,聊聊持续交付中的流水线模式

    人多力量大vs.两个披萨原则,聊聊持续交付中的流水线模式 在前面5期文章中,我们分别详细介绍了持续交付体系基础层面的建设,主要是多环境和配置管理,这些是持续交付自动化体系的基础,是跟我们实际的业务场景 ...

  2. Clang Static Analyzer-使用手册-编写Checker框架

    Clang Static Analyzer-使用手册-编写Checker Checker是这个工具的灵魂 有了checker才可以检查你的代码 相当于就是CSA通过checker定义的检查方法去检查代 ...

  3. SQLserver数据库安装教程

    大家好,这期给大家带来一期SQL server的安装教程 下载SQL Server 2019 Developer 官方网址: https://www.microsoft.com/zh-cn/sql-s ...

  4. 201871030102_崔红梅 实验三 结对项目—《D{0-1}KP 实例数据集算法实验平台》项目报告

    项目 内容 课程班级博客链接 班级博客 这个作业要求链接 作业要求 我的课程学习目标 1.体验软件项目开发中的两人合作,练习结对编程2. 掌握Github协作开发程序的操作方法.3.阅读<现代软 ...

  5. Spring Boot入门学习

    1. Spring Boot概述 1.1.什么是Spring Boot SpringBoot是一个可使用Java构建微服务的微框架.是Spring框架及其社区对"约定优先于配置"理 ...

  6. 浅入kubernetes(5):尝试kubeadm

    本篇介绍利用 kubernetes 的命令行工具,快速创建集群实例,完成 hello world 实践. 上一篇试用 minikube 去搭建集群,这一篇将介绍通过 kubeadm 去操作. 命令行工 ...

  7. pod详解

    什么是pod? 官方说明: Pod是Kubernetes应用程序的最基本执行单元-是你创建或部署Kubernetes对象模型中的最小和最简单的单元. Pod表示在集群上运行的进程.Pod封装了应用程序 ...

  8. sql指令,增,删,查,改

    增 insert into table (name,sex,age) value('张三','男','20')   向表中的name,sex,age,分别添加张三,男,20的内容 查 select  ...

  9. Scrum Meeting 目录 && alpha 第一次Scrum Meeting博客

    是兄弟就来摸鱼小组 Scrum Meeting 博客汇总 一.Alpha阶段 标号 标题 1 [alpha]第一次Scrum Meeting(见本文) 二.Beta阶段 会议安排 时间 4月23日8时 ...

  10. (数据科学学习手札119)Python+Dash快速web应用开发——多页面应用

    本文示例代码已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 这是我的系列教程Python+Dash快速web ...