与力扣 圆圈中最后剩下的数字 类似:https://leetcode.cn/problems/yuan-quan-zhong-zui-hou-sheng-xia-de-shu-zi-lcof/
将环加入数组中,每次计算index = (index + m - 1) % lists.size();
public int circle(int n, int m) { List<Integer> lists = new LinkedList<>(); for(int i = 1; i <= n; i++){ lists.add(i); } int index = 0; while (lists.size() > 1){ index = (index + m - 1) % lists.size(); lists.remove(index); } return lists.get(0); }
此问题容易超时,复杂度为0(n 2),涉及到每次遍历list删除index位置的元素。
标签:index,int,lists,约瑟夫,问题,每次,相关,size From: https://www.cnblogs.com/zzlback/p/16726723.html