forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
36 lines (29 loc) · 754 Bytes
/
main.cpp
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
30
31
32
33
34
35
36
/// Source : https://leetcode.com/problems/missing-number/description/
/// Author : liuyubobobo
/// Time : 2017-11-10
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
/// Sort and Check
/// Time Complexity: O(nlogn)
/// Space Complexity: O(1)
class Solution {
public:
int missingNumber(vector<int>& nums) {
sort(nums.begin(), nums.end());
int res = -1;
for(int i = 0 ; i < nums.size() ; i ++)
if(nums[i] != i){
res = i;
break;
}
return res == -1 ? nums.size() : res;
}
};
int main() {
int nums[3] = {0, 1, 3};
vector<int> vec(nums, nums + 3);
cout << Solution().missingNumber(vec) << endl;
return 0;
}