-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path45.圆圈中最后剩下的数字.cpp
More file actions
40 lines (38 loc) · 1.26 KB
/
45.圆圈中最后剩下的数字.cpp
File metadata and controls
40 lines (38 loc) · 1.26 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
class Solution {
public:
//O(n)递归解法
/*
int LastRemaining_Solution(unsigned int n, unsigned int m){
if(n < 1 || m < 1) return -1;
if(n == 1) return 0;
return (LastRemaining_Solution(n - 1, m) + m) % n;
}
*/
//传统模拟解法 O(mn), O(n)
int LastRemaining_Solution(unsigned int n, unsigned int m){
if(n < 1 || m < 1) return -1;
list<int> numbers;
for(int i = 0; i < n; ++i){
numbers.push_back(i);
}
list<int>::iterator current = numbers.begin();
while(--n){//控制循环,直到剩下一个结点
//得到将要删除的结点current
for(int i = 0; i < m - 1; ++i){
++current;
//若已经到链尾,则置下一个结点为链头
if(current == numbers.end())
current = numbers.begin();
}
//得到下一个起始结点(删除结点之后的结点)
list<int>::iterator next = current;
++next;
if(next == numbers.end())
next = numbers.begin();
//删除待删除结点
numbers.erase(current);
current = next;
}
return *current;
}
};