[Leetcode]Two Sum II - Input array is sorted
来源:互联网 发布:苹果手机下载不了淘宝 编辑:程序博客网 时间:2024/05/16 12:43
Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
You may assume that each input would have exactly one solution.
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
hint: double pointer, don't use (a+b) , this may be overflow
class Solution {public: vector<int> twoSum(vector<int> &numbers, int target) { vector<int>ret; int i,j; i = 0,j = numbers.size() - 1; while(i < j){ if(numbers[i] == (target - numbers[j])){ ret.push_back(i+1); ret.push_back(j+1); break; }else if(numbers[i] < (target - numbers[j])){ ++i; }else{ --j; } } return ret; }};
0 0
- leetcode 167: Two Sum II - Input array is sorted
- Leetcode NO.167 Two Sum II - Input array is sorted
- LeetCode-Two Sum II - Input array is sorted
- [Leetcode]Two Sum II - Input array is sorted
- leetcode--Two Sum II - Input array is sorted
- LeetCode 题解(274) : Two Sum II - Input array is sorted
- [LeetCode] Two Sum II – Input array is sorted
- Leetcode-167.Two Sum II - Input array is sorted
- [leetcode 167] Two Sum II - Input array is sorted
- Leetcode Two Sum II – Input array is sorted (Java)
- [leetcode] 167. Two Sum II - Input array is sorted
- LeetCode 167: Two Sum II - Input array is sorted
- 【leetcode】167. Two Sum II - Input array is sorted
- 【LeetCode】167. Two Sum II - Input array is sorted
- [LeetCode OJ]167. Two Sum II - Input array is sorted
- leetcode 167 Two Sum II - Input array is sorted C++
- [leetcode]167. Two Sum II - Input array is sorted
- 【Leetcode】Two Sum II - Input array is sorted
- hibernate入门实例
- poj2909 Goldbach's Conjecture
- 馨瑶
- 【Unity3d】关于打APK包勾选DevelopmentBuild摄像机正常,不勾则不正常问题
- c++ 字符串常量
- [Leetcode]Two Sum II - Input array is sorted
- 简易网页聊天室DEMO
- 网页设计常用术语解释
- java程序的运行机制
- C# 托管资源,非托管资源
- Ubuntu下使用SVN
- java.util.Date 转成相对时间 即几天前 几小时前形式
- create table DEFAULT あるとき、後にNOT NULL
- SectionIndexer中的getSectionForPosition()与getPositionForSection()解惑