关闭

leetcode167

标签: leetcodebs
110人阅读 评论(0) 收藏 举报
分类:

1、Two Sum II - Input array is sorted
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
思路跟二分法很相似,对于排好序的数组,一大一小从两头向中间走~~复杂度也只是O(n)哦~

class Solution {
public:
    vector<int> twoSum(vector<int>& numbers, int target) {
        vector<int> result;
        int length=numbers.size();
        int low=0,high=length-1;
        while(low<=high){
            if(numbers[high]+numbers[low]<target)
                ++low;
            else if(numbers[high]+numbers[low]>target)
                --high;
            else{
                result.push_back(low+1);
                result.push_back(high+1);
                break;
            }
        }
        return result;
    }
};
0
0
查看评论

leetcode167

Two Sum II - Input array is sorted
  • Ekinnn
  • Ekinnn
  • 2016-12-10 13:29
  • 169

LeetCode 167.Two Sum II 解题报告

LeetCode 167.Two Sum II 解题报告
  • camellhf
  • camellhf
  • 2016-09-18 13:43
  • 419

LeetCode oj 167. Two Sum II - Input array is sorted (双指针)

167. Two Sum II - Input array is sorted   Question Editorial Solution  My Submissions Total Accepted: 29576Total Submissions:&...
  • Sara_YF
  • Sara_YF
  • 2016-10-05 19:01
  • 238

Leetcode167: Bitwise AND of Numbers Range

Given a range [m, n] where 0 For example, given the range [5, 7], you should return 4. class Solution { public: int rangeBitwiseAnd(int m, ...
  • u013089961
  • u013089961
  • 2015-11-24 21:04
  • 172

leetcode167 Two SumII java

DescriptionGiven an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.Th...
  • u013190088
  • u013190088
  • 2017-03-09 10:49
  • 120

leetcode【1+167 Two Sum 系列】【python】

1 Two Sum找到给定序列中两个数字的和是指定target,返回的是个list,包含两个数的index,从0开始。第一反应肯定是遍历,毕竟是数组题,遍历需要两遍,才能找到和,那么肯定是要优化的了。 因为是查找,所以可以想到hash,查找只需要O(1)复杂度。那么维持一个dict,其中key是数...
  • u014251967
  • u014251967
  • 2016-09-01 19:31
  • 346

Leetcode-167. Two Sum II - Input array is sorted c语言

1、题目:点击打开链接 2、下面代码是暴力破解,超时无法通过: /** * Return an array of size *returnSize. * Note: The returned array must be malloced, assume caller calls free...
  • tzh476
  • tzh476
  • 2016-09-01 20:42
  • 566

[LeetCode167]Two Sum II - Input array is sorted

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 ...
  • u012175043
  • u012175043
  • 2015-11-24 05:58
  • 112

leetcode167~Two Sum II - Input array is sorted

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 functio...
  • stone_yw
  • stone_yw
  • 2017-03-09 10:52
  • 107

LeetCode167—Two Sum II - Input array is sorted

LeetCode167 Two Sum Input array is sorted
  • zhangxiao93
  • zhangxiao93
  • 2016-11-23 09:31
  • 327
    个人资料
    • 访问:9306次
    • 积分:665
    • 等级:
    • 排名:千里之外
    • 原创:59篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条