[刷题] 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), ...
随机推荐
- 阿里云 RTC QoS 弱网对抗之变分辨率编码
本文为 QoS 弱网优化系列的第二篇 作者|安基程.田伟峰 审校| 泰一 视频编码中的变分辨率问题及解决 变分辨率在弱网场景的实际应用中非常常见,网络状况不好的时候降低分辨率可以降低码率,减少块效应, ...
- 在ASP.NET Core中使用ViewComponent
前言 在之前的开发过程中,我们对于应用或者说使用一些小的组件,通常使用分布页(partial view),再往前在Web Form中我们会进行应用WEB Control,好吧提及一个关键性代码TagP ...
- 【Flutter 3-5】Flutter进阶教程——在Flutter中使用Lottie动画
作者 | 弗拉德 来源 | 弗拉德(公众号:fulade_me) Lottie动画 在移动开发中总是需要展示一些动画特效,作为程序员的我们并不是很擅长用代码做动画,即便是有些动画可以实现,在跨平台的过 ...
- Dynamics CRM安装教程六:CRM产品安装
接下来就要开始进行CRM产品的安装了 首先要安装IIS,以及.NET FrameWork4.6及相关功能 打开服务器管理器,在添加角色功能向导中勾选IIS,点击添加功能 默认下一步 选择角色服务这里的 ...
- 体渲染——Volume
基本概念 体渲染(Volume),是绘制类似烟.雾.云的效果.这种渲染和之前的表面渲染不同,光线可以在物体内部进行散射. 体渲染的主要特点 1. 可以在物体内部散射. 2. 从进入vo ...
- 浅谈 Fresco 框架结构
在前面的文章 Fresco 源码分析 -- 图片加载流程 里面详细说明了图片加载的整个流程,但是除了理解源码之外,对于源码的框架层面的设计也是需要去了解的,不能只是简单的读源码,好的源码的框架设计也是 ...
- Dropping Balls UVA - 679
A number of K balls are dropped one by one from the root of a fully binary tree structure FBT. Eac ...
- .Net下引用多目标框架项目导致XAML无法识别类型的问题
背景 我的项目(以下称为LibA)中有一个多目标框架编译的项目,即项目文件包含如下代码: <PropertyGroup> ... <TargetFrameworks>netco ...
- 解决小程序中Data.parse()获取时间戳IOS不兼容
由于与后台接口必须对比时间戳所以首先得前台获取时间戳.刚开始是获取手机本地时间,但用户改了时间就废了..... 后来就从服务器上获取个时间再转换为时间戳(是不是很操蛋,先从服务器上获取在TM的自己比较 ...
- hdu4496并查集的删边操作
题意: 给你一个图,问你删除一些边后还有几个连通快.. 思路: 典型的并查集删边操作,并查集的删边就是先把不删除的边并查集一边(本题没有不删除的边),然后逆序吧所有要删除的边以 ...