aboutsummaryrefslogtreecommitdiff
path: root/store/works/solutions/leetcode/cpp/que-shi-de-shu-zi-lcof.cpp
blob: 1cb3e015a947db4f6ab2bff7404832a6f62a071e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <vector>

using std::vector;

class Solution
{
public:
    int missingNumber(vector<int> &nums)
    {
        if (nums.back() == nums.size() - 1)
            return nums.size();

        int low = 0, high = nums.size() - 1;
        while (low != high)
        {
            int middle = (low + high) / 2;
            if (middle == nums[middle])
            {
                low = middle + 1;
            }
            else
            {
                high = middle;
            }
        }

        return low;
    }
};