递归写法
class Solution {
public:
int lastRemaining(int n, int m){
if (n == 1) return 0;
return (lastRemaining(n - 1, m) + m) % n;
}
};
迭代写法
class Solution {
public:
int lastRemaining(int n, int m){
if (n == 1) return 0;
int ret = 0;
for (int i = 1; i < n;) {
i ++ ;
ret = (ret + m) % i;
}
return ret;
}
};