-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path268.missing-number.cpp
More file actions
49 lines (48 loc) · 983 Bytes
/
268.missing-number.cpp
File metadata and controls
49 lines (48 loc) · 983 Bytes
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
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
* @lc app=leetcode id=268 lang=cpp
*
* [268] Missing Number
*/
// @lc code=start
#include <vector>
using namespace std;
class Solution
{
public:
int missingNumber(vector<int> &nums)
{
int p = 0;
int tmp;
int val;
int n = nums.size();
int n_val = -1;
int last_ptr = n;
while (p < nums.size())
{
val = nums[p];
while (val != p)
{
if (val == -1)
{
last_ptr = p;
break;
}
else if (val == n)
{
tmp = n_val;
n_val = n;
}
else
{
tmp = nums[val];
nums[val] = val;
}
val = tmp;
}
nums[p] = val;
p++;
}
return last_ptr;
}
};
// @lc code=end