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. Ubuntu中wine安装的程序如何卸载

    很多朋友尝试在Ubuntu中用wine安装exe格式的应用程序,但经常遇到装完之后启动程序就崩溃.或者根本无法启动.无法使用的情况,于是想立即把安装的程序卸载,可是在wine中却找不到卸载exe软件的 ...

  2. python学习day2--python基础

    python没有常量的定义方式,一般常量可人为用全部大写字母来表示. 如: MYSQL_CONNECTION='192.168.10.2' 以os库为例: import os os.system('i ...

  3. 有关开机后win7任务管理器不断重启的问题,我的情况是sendrpt.exe导致的(转载,有补充)

    SendRpt.exe提示SendRpt:Error资源管理器未响应 打开电脑就发现资源管理器就未响应,还发现一个标题为Report sending utility的SendRpt.exe进程占用CP ...

  4. Struts升级到2.3.15.1抵抗漏洞

    后知后觉,今天才开始修复Struts2的漏洞 详细情形可以参考: http://struts.apache.org/release/2.3.x/docs/security-bulletins.html ...

  5. IOS 异步GET方法请求

    1.添加协议NSURLConnectionDelegate 2.引入头文件“NSString+URLEncoding”,用来处理URL进行编码. 3.引入头文件“NSNumber+Message”,用 ...

  6. 【译】 Node.js v0.12的新特性 -- 性能优化

    原文: https://strongloop.com/strongblog/performance-node-js-v-0-12-whats-new/ January 21, 2014/in Comm ...

  7. OpenCV基本架构[OpenCV 笔记0]

    最近正在系统学习OpenCV,将不定期发布笔记,主要按照毛星云的<OpenCV3编程入门>的顺序学习,会参考官方教程和文档.学习工具是Xcode+CMake,会对书中一部分内容更正,并加入 ...

  8. CSMA-CA介绍

    本文主要介绍通讯领域中CSMA相关机制,本文全部资料来自于网络.   网络通讯,必须依靠介质来传递数据,将数据调制到模拟信号上,再把此信号通过介质传递到远方.根据介质的不同,分为有线网络和无线网络.为 ...

  9. lex&yacc

    LEX: yytext 数组包含匹配模式的文本; 使词法分析程序工作的两条规则是:1. lex 模式只匹配输入字符或字符串一次.2. lex 执行当前输入的最长可能匹配的动作. 由 lex 产生的词法 ...

  10. bzoj 3626 LCA

    这一道题咋一看只觉得是离线,可以求出所有的f(1,i,z), 答案就等于f(1,r,z)-f(1,l-1,z).但是没有具体的做法,但是求LCA的深度和有一个非常巧妙的做法,每加一个点,就把这个点到根 ...