-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind_missing_number.cpp
More file actions
67 lines (47 loc) · 1.08 KB
/
find_missing_number.cpp
File metadata and controls
67 lines (47 loc) · 1.08 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
给定一个数组,大小为98,里面的元素均为在[0, 99],且无重复,找出缺失的两个元素值。
要求:不能申请额外的空间,时间复杂度为O(N)
*/
#include <vector>
#include <iostream>
using namespace std;
void swap(int& in1, int& in2)
{
int tmp;
tmp = in1;
in1 = in2;
in2 = tmp;
}
void find_missing(int* in, int size)
{
for(int i = 0; i < size; i++)
{
while (in[i] != i) {
if (in[i] >= size || in[i] < 0)
break;
cout<<"i "<<i<<" in[i]"<<in[i]<<endl;
int tmp = in[i];
in[i] = in[tmp];
in[tmp] = tmp;
}
}
for(int i = 0; i < size; i++)
{
if (in[i] != i)
cout<<"missing "<<i<<endl;
}
}
int main()
{
int test[98];
for (int i = 0; i < 98; i ++)
test[i] = i;
test[9] = 99;
test[10] = 98;
swap(test[9], test[0]);
swap(test[10], test[5]);
for (int i = 0; i < 98; i ++)
cout<<"test["<<i<<"]"<<"="<<test[i]<<endl;
find_missing(test, 98);
return 0;
}