题解 | #环形链表的约瑟夫问题#
环形链表的约瑟夫问题
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;
}
};
文远知行公司福利 509人发布