package leetcode;

import java.util.Random;

/**
 * https://leetcode.com/problems/random-flip-matrix/
 */
public class Problem519 {
    class Solution {
        private final Random random = new Random();
        private int[][] matrix;
        private final int nRows;
        private final int nCols;

        public Solution(int n_rows, int n_cols) {
            nRows = n_rows;
            nCols = n_cols;
            matrix = new int[n_rows][n_cols];
        }

        public int[] flip() {
            int r = random.nextInt(nRows);
            int c = random.nextInt(nCols);
            while (matrix[r][c] == 1) {
                r = random.nextInt(nRows);
                c = random.nextInt(nCols);
            }
            matrix[r][c] = 1;
            return new int[]{r, c};
        }

        public void reset() {
            matrix = new int[nRows][nCols];
        }
    }

    public static void main(String[] args) {
        Problem519 prob = new Problem519();
    }
}

@@ -29,22 +29,70 @@
 va.util.
-Random
+HashSet;%0Aimport java.util.Random;%0Aimport java.util.Set
 ;%0A%0A/**%0A 
@@ -177,16 +177,31 @@
 9 %7B%0A    
+private static 
 class So
@@ -281,22 +281,41 @@
 ate 
-int%5B%5D%5B%5D matrix
+Set%3CString%3E set = new HashSet%3C%3E()
 ;%0A  
@@ -489,54 +489,8 @@
 ls;%0A
-            matrix = new int%5Bn_rows%5D%5Bn_cols%5D;%0A
     
@@ -628,32 +628,68 @@
     
-while (matrix%5Br%5D%5Bc%5D == 1
+String key = key(r, c);%0A            while (set.contains(key)
 ) %7B%0A
@@ -790,159 +790,197 @@
     
-%7D%0A            matrix%5Br%5D%5Bc%5D = 1;%0A            return new int%5B%5D%7Br, c%7D;%0A        %7D%0A%0A        public void reset() %7B%0A            matrix = new int%5BnRows%5D%5BnCols%5D
+    key = key(r, c);%0A            %7D%0A            set.add(key);%0A            return new int%5B%5D%7Br, c%7D;%0A        %7D%0A%0A        private String key(int row, int col) %7B%0A            return row + %22%7C%22 + col
 ;%0A  
@@ -987,23 +987,21 @@
       %7D%0A
+%0A
     
-%7D%0A%0A
     publ
@@ -1007,85 +1007,67 @@
 lic 
-static void main(String%5B%5D args) %7B%0A        Problem519 prob = new Problem519();
+void reset() %7B%0A            set = new HashSet%3C%3E();%0A        %7D
 %0A   

Solve problem 519