题解 | #孩子们的游戏(圆圈中最后剩下的数)#
孩子们的游戏(圆圈中最后剩下的数)
https://www.nowcoder.com/practice/f78a359491e64a50bce2d89cff857eb6
class Solution {
public:
int LastRemaining_Solution(int n, int m) {
queue<int> res;
for(int i=0;i<n;i++) res.push(i);
while(res.size() != 1){
int count = 0;
while(1){
int head = res.front();
count++;
if(count == m){
res.pop();
break;
}
res.push(head);
res.pop();
}
}
return res.front();
}
};//空间O n 时间O mn
查看13道真题和解析