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 ...
随机推荐
- sql常识-top
TOP 子句 TOP 子句用于规定要返回的记录的数目. 对于拥有数千条记录的大型表来说,TOP 子句是非常有用的. 注释:并非所有的数据库系统都支持 TOP 子句. SQL Server 的语法: S ...
- .net转java了
公司技术部门 要求.net全体转向java 本来要看看.net core的 看来是没必要了 现在国内互联网公司.net是越来越少 不知道为何会这样 不过java的生态圈 确实是很强大 也很丰富 ...
- js 后台弹窗
后台弹出操作成功,失败信息 /// <summary> /// 弹出信息,并跳转指定页面. /// </summary> public static void AlertAnd ...
- plsqldev与sqldeveloper
plsqldev连接 1.连接不同服务器,要修改tnsnames.ora文件,具体如下修改如下位置 # tnsnames.ora Network Configuration File: \app\us ...
- jQuery 笔记
1. 选择器 http://www.runoob.com/jquery/jquery-selectors.html 2. toggle() 用来切换 hide() 和 show() 方法 ht ...
- Java多线程(一) 多线程的基本使用
在总结JDBC数据库连接池的时候,发现Java多线程这块掌握得不是很好,因此回头看了下多线程的内容.做一下多线程模块的学习和总结,稳固一下多线程这块的基础.关于多线程的一些理论知识,这里不想啰嗦太多, ...
- 【转载】GDB反向调试(Reverse Debugging)
记得刚开始学C语言的时候,用vc的F10来调试程序,经常就是一阵狂按,然后一不小心按过了.结果又得从头再来,那时候我就问我的老师,能不能倒退回去几步.我的老师很遗憾地和我说,不行,开弓没有回头箭.这句 ...
- 【Winform】 Enum逆向解析
将字符串转换成Enum类型 Enum.Parse:将一个或多个枚举常数的名称或数字值的字符串表示转换成等效的枚举对象. 名称 说明 Parse(Type, String) 将一个或多个枚举常数 ...
- 浏览器内核-Webkit
关键字:浏览器内核,浏览器引擎,Browser,Webkit,Blink,Chromium. 本文简单介绍一下各种浏览器内核.着种介绍一下Webkit.顾名思义,浏览器内核就是浏览器的核心部分,也可以 ...
- 关于Simple.Data.PostgreSql的ExecuteReader没实现非常坑爹的问题
https://github.com/ChrisMH/Simple.Data.PostgreSql/issues/3 github上有个issues...默认从nuget上下载的Simple.Data ...