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 ...
随机推荐
- EF4.1之Code first 的几种连接数据库的方式
通过代码 进行连接和创建数据库的方法主要分为两种: 1.使用用连接字符串(在配置文件里面): 连接字符串: <add name="DbEntities" connection ...
- 改变UITextField placeHolder 字体 颜色
[_textSearchField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"]; ...
- MySQL页面打捞工具使用方法
MySQL数据打捞工具 0.1 windows版 下载 一,选择数据源与输出目录 数据源可以是分区或物理物理磁盘,如\\.\D: 或\\.\PhysicalDrive0; 二,参数设置 请设置扫描参数 ...
- From MSI to WiX, Part 8 - Major Upgrade, by Alex Shevchuk
Following content is reprinted from here, please go to the original website for more information. Au ...
- 真正的inotify+rsync实时同步 彻底告别同步慢
真正的inotify+rsync实时同步 彻底告别同步慢 http://www.ttlsa.com/web/let-infotify-rsync-fast/ 背景 我们公司在用in ...
- Ubuntu 下 glpk 的安装及使用
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4156204.html glpk是一个开源的求解线性规划的包. 添加源: deb http:/ ...
- C#对象转JSON字符串和JSON字符串转对象
namespace Net.String.ConsoleApplication { using System; using System.Data; using System.Collections; ...
- dapper 自定义数据库字段和代码中Model字段不一致时候的mapping方法
namespace YourNamespace { /// <summary> /// Uses the Name value of the ColumnAttribute specifi ...
- 009.EscapeRegExChars
类型:function 可见性:public 所在单元:RegularExpressionsCore 父类:TPerlRegEx 把转义字符变成原意字符 例如\d意为0~9某个数字,通过此函数转换后则 ...
- Catalyst揭秘 Day1 Catalyst本地解析
Catalyst揭秘 Day1 Catalyst本地解析 今天开始讲下Catalyst,这是我们必须精通的内容之一: 在Spark2.x中,主要会以Dataframe和DataSet为api,无论是D ...