1.题目
https://leetcode-cn.com/problems/yuan-quan-zhong-zui-hou-sheng-xia-de-shu-zi-lcof/
2.分析
每杀掉一个人,其实就是把这个数组向前移动了M位。然后逆过来,就可以得到这个递推式。 约瑟夫环
3.代码
class Solution {
public int lastRemaining(int n
, int m
) {
int k
=0;
for(int i
=1;i
<n
;i
++){
k
=(k
+m
)%(i
+1);
}
return k
;
}
}
4.复杂度
时间复杂度:O(N) 空间复杂度:O(1)
5.结果