leetcode2 Two Sum II – Input array is sorted
Two Sum II – Input array is sorted
whowhoha@outlook.com
Question:
Similar to Question [1. Two Sum], except that the input array is already sorted in
ascending order.
同上题:先排序,然后从开头和结尾同时向中间查找,原理也比较简单。O(nlogn) runtime, O(1) space
vector<int> twoSumSored(vector<int>& nums, int target){
vector<int> vecCopy(nums);
int i=0,n=2,l=0,r = nums.size()-1;
sort(vecCopy.begin(),vecCopy.end());
int j=nums.size()-1;
while(i<j)
{
int sum = nums[i]+nums[j];
if (sum <target)
{
i++;
}
else if (sum > target)
{
j--;
}
else
{
vector<int> index;
index.push_back(i+1);
index.push_back(j+1);
return index;
}
}
}
leetcode2 Two Sum II – Input array is sorted的更多相关文章
- 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 ...
- 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 ...
- Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)
Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted) 给定一个已按照升序排列 的有序数组,找到两个数使得它们 ...
- 【LEETCODE】38、167题,Two Sum II - Input array is sorted
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- LeetCode_167. Two Sum II - Input array is sorted
167. Two Sum II - Input array is sorted Easy Given an array of integers that is already sorted in as ...
- 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 - Input array is sorted 两数和 II - 输入是有序的数组
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- 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 ...
随机推荐
- HTTP 错误 500.21 - Internal Server Error 处理程序“PageHandlerFactory-Integr
将网站发布到IIS,访问发生如下错误: HTTP 错误 500.21 - Internal Server Error处理程序"PageHandlerFactory-Integr"在 ...
- C# list使用方法
C# list使用方法 集合是OOP中的一个重要概念,C#中对集合的全面支持更是该语言的精华之一. 为什么要用泛型集合? 在C# 2.0之前,主要可以通过两种方式实现集合: a.使用ArrayList ...
- Using LINQ Group By and String.Join() / Aggregate() in Entity Framework 3.5
linq to sql 的时候,有时候需要用到 先group 然后来个 aggregate 串连一下值, 但会总会出错,说不识别 aggregate 或者 string.join 方法 搜遍网络 一 ...
- WebGIS基础复习笔记
明天要考试了,突击一下. 1.万维网:www是world wide web的简称是在超文本基础上形成的信息网 2.互联网:即广域局域网及单机按照一定的通讯协议组成的国际计算机网络 3.WebGIS:网 ...
- CLI结果输出
例子:FTP::11.245.253.20_CIPS_dev_bak\/opt/IBM/db2/V9.7/samples/ 要不要修改整体结构,先看看细节 CLI结果输出: 1. 逐条的获取--确定产 ...
- COM 学习小记录
COM组件程序:模块,它可以是 动态连接库(DLL) && 可执行程序(EXE),称为 进程内组件(in-of-process component) && 进程外组件( ...
- 登堂入室——java流
——文章出自PeterYe,不得私自转载 我所知道的 java.io里面的[流],就仿佛太平洋里面的水一样,浩浩荡荡,横无际涯... -----2016/7/16--------公寓处记录------ ...
- Linux 下添加普通用户,登陆并删除
adduser 命令.LINUX创建用户的命令useradd -g test -d /home/test1 -s /etc/bash -m test1注解:-g 所属组 -d 家目录 -s 所用的SH ...
- jquery——ajax加载后的内容,单击事件失效
使用delegate(),on()绑定事件,可以将事件绑定到其祖先元素上,这样以后加载出来的元素,单击事件仍然有效
- jQuery实现页内查找相关内容
当需要在页面中查找某个关键字时,一是可以通过浏览器的查找功能实现,二是可以通过前端脚本准确查找定位,本文介绍通过jQuery实现的页面内容查找定位的功能,并可扩展显示查找后的相关信息. 本文以查找车站 ...