leetcode167. Two Sum II - Input array is sorted

Posted Militant_799

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode167. Two Sum II - Input array is sorted相关的知识,希望对你有一定的参考价值。

题意:在排序的数组,找到两个元素和为target,返回其下标index1 和 index2 (1<=index1 < index2),只有一种答案,每个元素不能用两次。

分析:

using namespace std;
class Solution
{
public:
    vector<int> twoSum(vector<int>& numbers, int target)
    {
        int len = numbers.size();
        vector<int>ret;
        int flag = 0;
        for (int i=0; i<len; i++)
        {
            for (int j = i+1; j<len; j++)
            {
                if (numbers[i] + numbers[j] == target)
                {
                    ret.push_back(i);
                    ret.push_back(j);
                    flag = 1;
                }
                if (flag ) break;
            }
            if (flag) break;
        }
        return ret;
    }
};

  

以上是关于leetcode167. Two Sum II - Input array is sorted的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode167. 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 #167. Two Sum II - Input array is sorted

Leetcode167. Two Sum II - Input array is sorted