LeetCode 剑指 Offer 62. 圆圈中最后剩下的数字

约瑟夫环问题。
思路:一个一个杀掉,再逆向补回来还原位置。
参考解释

int lastRemaining(int n, int m){
    
    
    int pos = 0;
    for(int i = 2; i <= n; i++){
    
    
        pos = (pos + m) % i;
    }
    return pos;
}

猜你喜欢

转载自blog.csdn.net/qq_43078427/article/details/110748473