Java | Leetcode Java题解之第519题随机翻转矩阵
题目:
题解:
class Solution {Map<Integer, Integer> map = new HashMap<>();int m, n, total;Random rand = new Random();public Solution(int m, int n) {this.m = m;this.n = n;this.total = m * n;}public int[] flip() {int x = rand.nextInt(total);total--;// 查找位置 x 对应的映射int idx = map.getOrDefault(x, x);// 将位置 x 对应的映射设置为位置 total 对应的映射map.put(x, map.getOrDefault(total, total));return new int[]{idx / n, idx % n};}public void reset() {total = m * n;map.clear();}
}