题目
类型:HashTable
解题思路
代码
class Solution {
int m, n, cnt; // cnt 为剩余数个数,同时 cnt - 1 为区间右端点位置
Map<Integer, Integer> map = new HashMap<>();
Random random = new Random(300);
public Solution(int _m, int _n) {
m = _m; n = _n; cnt = m * n;
}
public int[] flip() {
int x = random.nextInt(cnt--);
int idx = map.getOrDefault(x, x);
map.put(x, map.getOrDefault(cnt, cnt));
return new int[]{idx / n, idx % n};
}
public void reset() {
cnt = m * n;
map.clear();
}
}
/**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(m, n);
* int[] param_1 = obj.flip();
* obj.reset();
*/