题解 | #环形链表的约瑟夫问题#

环形链表的约瑟夫问题

https://www.nowcoder.com/practice/41c399fdb6004b31a6cbb047c641ed8a

class Solution {
public:
    /**
     * 
     * @param n int整型 
     * @param m int整型 
     * @return int整型
     */

     
    int ysf(int n, int m) {
        // write code here
        queue<int> ans;
        for(int i=1;i<=n;++i){ans.push(i);}
        
        int han=1;
        int xx;
        while(true){
            int now = ans.front();
            ans.pop();
            if(han==m){
                han = 1;
                if(ans.empty()){
                    xx=now;
                    break;
                }

            }
            else{
                han+=1;
                ans.push(now);
            }
        } 
        return xx;
    }
};

全部评论

相关推荐

learYuan:🐕看了都摇头
点赞 评论 收藏
分享
人生一梦:24年我投暑期实习,它以我不是女的为理由拒绝了我查看图片
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务