LC.P2682[找出转圈游戏输家]

方法一:模拟

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public int[] circularGameLosers(int n, int k) {
boolean[] visited = new boolean[n];
int cnt = 0;
for (int i = 0, t = 1; !visited[i]; ++t) {
visited[i] = true;
++cnt;
i = (i + t * k) % n;
}
int[] ans = new int[n - cnt];
for (int i = 0, j = 0; i < n; ++i) {
if (!visited[i]) {
ans[j++] = i + 1;
}
}
return ans;
}
}
  • 时间复杂度:$O(n)$
  • 空间复杂度:$O(n)$