当前位置:网站首页>62. the last number left in the circle

62. the last number left in the circle

2022-06-12 05:18:00 Be your goat

The finger of the sword Offer 62. The last number in the circle

Ideas : mathematics

[0,1,2,3,4]

Delete the... Every time m Number , Back to :

Fourth Backstepping : Fill up m A place , Current array size on module 2

The third thrust : Fill up m A place , Current array size on module

summary , Namely (index+m)% Current array size

class Solution {
public:
    int lastRemaining(int n, int m) {
        int res=0;
        for(int i=2;i<=n;++i){
            res=(res+m)%i;
        }
        return res;
    }
};

Time complexity O(n)

Spatial complexity O(1)

原网站

版权声明
本文为[Be your goat]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203010618459263.html

随机推荐