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. iOS 在viewController中监听Home键触发以及重新进入界面的方法

    第一步:创建2个NSNotificationCenter监听 [[NSNotificationCenter defaultCenter] addObserver:self selector:@sele ...

  2. kettle列转行

  3. 谷歌(Chrome)安装Advanced REST Client插件

    进入Extensions(工具——>扩展程序) 点击Get More extensions或新建标签页点击网上应用店 如果加载太慢,出现chrome网上应用店无法打开,显示暂时无法加载该应用的画 ...

  4. UIDynamic 基础认识

    UIDynamic 是从iOS 7开始引入的一种新技术,属于UIKit框架,可以模拟现实生活中的物理现象,如:碰撞.抖动.摆动等 动力效果:有一个效果器,叫做“动力效果器”里面面可以添加“动力效果” ...

  5. C# DateTimePicker控件详解

    1.同时显示日期和时间 DateTimePicker dtp = new DateTimePicker(); dtp.Format = DateTimePickerFormat.Custom;dtp. ...

  6. 基于asp.net的Web开发架构探索

    问题由来 最近在研究适合团队开发的web架构解决方案,该架构即要适合分工协作又要有一定扩展性,适合不同的数据库需要,因此我查阅了一些资料,初步构想出了一套架构,请各位多多指教. 探索 web开发架构最 ...

  7. String str 与 String str=new String("") 区别

    1.当使用String str="abc",这种方式时,先去内存的Heap中找是否存在"abc"这个字符串,若存在,则将地址引用.若不存在则创建. 2.当使用S ...

  8. zz Release memory in Linux (Unused or Cached)

    In computer science, Memory Leakage occurs when a computer software or program consumes required mem ...

  9. [GeekBand] STL与泛型编程(2)

    本篇文章在上一篇文章的基础上进一步介绍一些常用的容器以及STL的一些深入知识. 一. Stack和Queue 栈和队列是非常常用的两种数据结构,由deque适配而来.关于数据结构的知识这里就不在介绍了 ...

  10. OpenCV3添加滑动条和鼠标事件到图形界面

    鼠标事件和滑动条控制在计算机视觉和OpenCV中非常有用,使用这些控件,用户可以直接与图形界面交互,改变输入图像或者变量的属性值. /* In this section, we are going t ...