题解 | #环形链表的约瑟夫问题#
环形链表的约瑟夫问题
http://www.nowcoder.com/practice/41c399fdb6004b31a6cbb047c641ed8a
public class Solution {
/**
*
* @param n int整型
* @param m int整型
* @return int整型
*/
public int ysf (int n, int m) {
// write code here
if(n == 1 && m == 1){
return 1;
}
ArrayList<Integer> list = new ArrayList<>();
for( int i = 1; i <= n; i++){
list.add(i);
}
int start = 0;
int del = 0;
while(list.size() != 1){
// 找到要移除的位置
del = (start + m -1) % list.size();
start = del;
list.remove(del);
}
return list.get(0);
}
}