-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_0448_findDisappearedNumbers.cc
71 lines (64 loc) · 1.16 KB
/
Problem_0448_findDisappearedNumbers.cc
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
66
67
68
69
70
71
#include <iostream>
#include <vector>
#include "UnitTest.h"
using namespace std;
class Solution
{
public:
vector<int> findDisappearedNumbers(vector<int> &nums)
{
vector<int> ans;
if (nums.size() == 0)
{
return ans;
}
int N = nums.size();
for (int i = 0; i < N; i++)
{
walk(nums, i);
}
for (int i = 0; i < N; i++)
{
if (nums[i] != i + 1)
{
ans.push_back(i + 1);
}
}
return ans;
}
// 下标循环怼
void walk(vector<int> &nums, int i)
{
while (nums[i] != i + 1)
{
int nexti = nums[i] - 1;
if (nums[nexti] == nexti + 1)
{
break;
}
swap(nums, i, nexti);
}
}
void swap(vector<int> &nums, int i, int j)
{
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
};
void testFindDisappearedNumbers()
{
Solution s;
vector<int> n1 = {4, 3, 2, 7, 8, 2, 3, 1};
vector<int> o1 = {5, 6};
vector<int> n2 = {1, 1};
vector<int> o2 = {2};
EXPECT_TRUE(o1 == s.findDisappearedNumbers(n1));
EXPECT_TRUE(o2 == s.findDisappearedNumbers(n2));
EXPECT_SUMMARY;
}
int main()
{
testFindDisappearedNumbers();
return 0;
}