[刷题] 167 Two Sum II
要求
- 升序数组
- 找到两个数使得它们相加之和等于目标数
- 函数返回两个下标值(下标从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的更多相关文章
- 【LEETCODE】38、167题,Two Sum II - Input array is sorted
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 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 ...
- 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
167. Two Sum II - Input array is sorted Given an array of integers that is already sorted in ascendi ...
- 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 ...
- 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 ...
- 167. Two Sum II - Input array is sorted - LeetCode
Question 167. Two Sum II - Input array is sorted Solution 题目大意:和Two Sum一样,这里给出的数组是有序的 思路:target - nu ...
- [LeetCode] #167# Two Sum II : 数组/二分查找/双指针
一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...
- 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), ...
随机推荐
- Spring Cloud 升级之路 - 2020.0.x - 2. 使用 Undertow 作为我们的 Web 服务容器
本项目代码地址:https://github.com/HashZhang/spring-cloud-scaffold/tree/master/spring-cloud-iiford 在我们的项目中,我 ...
- SQLserver数据库安装教程
大家好,这期给大家带来一期SQL server的安装教程 下载SQL Server 2019 Developer 官方网址: https://www.microsoft.com/zh-cn/sql-s ...
- Sqlmap的基础用法(禁止用于非法用途,测试请自己搭建靶机)
禁止用于非法用途,测试与学习请自己搭建靶机 sqlmap -r http.txt #http.txt是我们抓取的http的请求包 sqlmap -r http.txt -p username #指 ...
- c# 输出一个数组
关于C#输出一个数组最普遍的方法就是用for 循环语句写 如: int[] a = new int[10];for (int i = 0; i < a.Length; i++) { a[i] = ...
- 一文简述Java IO
Java IO 本文记录了在学习Java IO过程中的知识点,用于复习和快速查阅,不够详细的部分可能会在后续补充. 什么是流 流:内存与存储设备(外存)之间传输数据的通道 IO:输入流输出流(如rea ...
- 浅谈 Fresco 框架结构
在前面的文章 Fresco 源码分析 -- 图片加载流程 里面详细说明了图片加载的整个流程,但是除了理解源码之外,对于源码的框架层面的设计也是需要去了解的,不能只是简单的读源码,好的源码的框架设计也是 ...
- (一)Docker-in-Docker on Kubernetes
1. 场景 请参考docker in docker 文章 2. DinD 我们将采用主机Docker守护程序作为外部守护程序,Docker守护程序作为内部守护程序在容器内运行.运行DinD的一个重要方 ...
- Day03_17_数组
数组 什么是数组? 数组是多个相同类型数据按照一定顺序排列的有序集合,并使用一个名字命名,通过编号的方式,对这些数据进行统一的管理. 数组也是对象,数组中的元素相当于数组对象的成员变量 数组的长度是固 ...
- 请求转发(forward)和请求包含(include)的区别?
请求包含的例子 第一个Servlet (DispatcherServlet) @Override protected void doGet(HttpServletRequest req, HttpSe ...
- vue中利用.env文件存储全局环境变量,以及配置vue启动和打包命令
目录 1,前言 2,.env文件的作用 3,配置.env文件 4,配置启动命令 5,获取.env中的全局变量 5,实际用处 1,前言 分享一下vue项目中利用.env文件存储全局环境变量,以及利于项目 ...