Skip to content

Files

Latest commit

 

History

History
49 lines (40 loc) · 1.04 KB

Question_519.md

File metadata and controls

49 lines (40 loc) · 1.04 KB

LeetCode Records - Question 519 Random Flip Matrix

Attempt 1: Use a HashSet to store the flipped positions

class Solution {

    private int m;
    private int n;
    private Random random;
    private Set<String> set;

    public Solution(int m, int n) {
        this.m = m;
        this.n = n;
        this.random = new Random();
        this.set = new HashSet<>();
    }
    
    public int[] flip() {
        int x = random.nextInt(m);
        int y = random.nextInt(n);
        String str = x + " " + y;

        while (set.contains(str)) {
            x = random.nextInt(m);
            y = random.nextInt(n);
            str = x + " " + y;
        }

        set.add(str);
        return new int[]{ x, y };
    }
    
    public void reset() {
        set = new HashSet<>();
    }
}

/**
 * Your Solution object will be instantiated and called as such:
 * Solution obj = new Solution(m, n);
 * int[] param_1 = obj.flip();
 * obj.reset();
 */
  • Runtime: 28 ms (Beats: 29.03%)
  • Memory: 45.59 MB (Beats: 45.04%)