题解 | #环形链表的约瑟夫问题#
环形链表的约瑟夫问题
http://www.nowcoder.com/practice/41c399fdb6004b31a6cbb047c641ed8a
用list模拟离开回环的情况,直到剩下最后一个为止
import java.util.*; public class Solution { public int ysf (int n, int m) { List<Integer> list = new ArrayList<>(); for(int i = 1 ; i<= n ;i++){ list.add(i); } int begin = 0; while(list.size()!=1){ begin = (m+begin-1)%list.size(); list.remove(begin); } return list.get(0); } }