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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)

    Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted) 给定一个已按照升序排列 的有序数组,找到两个数使得它们 ...

  5. 【LEETCODE】38、167题,Two Sum II - Input array is sorted

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

  6. 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 ...

  7. 167. Two Sum II - Input array is sorted - LeetCode

    Question 167. Two Sum II - Input array is sorted Solution 题目大意:和Two Sum一样,这里给出的数组是有序的 思路:target - nu ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. mysql补集合计算

    mysql补集计算方法:   两表是1对多关系,user_id是关联字段,两表的数据量都是千万级别的     子查询实现 select count(*),sum(total_money) from A ...

  2. ###《VIM实用技巧》

    ###<VIM实用技巧> #@author: gr #@date: 2015-11-20 #@email: forgerui@gmail.com <VIM实用技巧>阅读笔记. ...

  3. (转)实战Memcached缓存系统(8)Memcached异步实时读写问题的解决方案SAC

    在使用Memcached时,一般实时读写的场景并不多见.但多是Memcached写入后,在一定时间后才会有读操作.但是如果应用场景,是写入后瞬间即会有读操作呢?似乎没有什么特别之处,我们依然可以这样写 ...

  4. 【原】从/dev/null重新打开标准输出

    今天遇到一个程序,使用了printf输出中间的信息,我也懒得去改.由于此进程被其他进程fork之后,dup2 了标识输入输出到了/dev/null,再通过execvp装载进来.于是,为了看到输出的信息 ...

  5. linux环境下配置github远程仓库

    1.设置git用户和邮箱 git config --global user.name "fujinzhou" git config --global user.email &quo ...

  6. 第六章 Qt布局管理器Layout

    第六章 Qt布局管理器Layout 大家有没有发现一个现象,我们放置一个组件,给组件最原始的定位是给出这个控件的坐标和宽高值,这样Qt就知道这个组件的位置.当用户改变窗口的大小,组件还静静地呆在原来的 ...

  7. javascript显示倒计时控制按钮

    html: <a><span id="sendAgain" onclick="sendEmail()">2.再次发送激活邮件</s ...

  8. 【Qt】关于Qt【转】

    什么是Qt Qt是一个针对桌面.嵌入式.移动设备的一个跨平台的应用程序开发框架,支持的平台包括Linux.OS X.Windows.VxWorks.QNX.Android.iOS.BlackBerry ...

  9. thymeleaf 内联语法

    十二. thymeleaf内联语法 内联:th:inline,值有三种:text,javascript,none 12.1 th:inline="text"文本内联 <p t ...

  10. mac下安装pcntl

     Now you need to find out what version of PHP is installed on OSX $ php -vPHP 5.3.10 with Suhosin-Pa ...