My easy-to-understand Java solution by using HashSet


  • 2
    public class Solution {
        public void setZeroes(int[][] matrix) {
            int m = matrix.length; 
            int n = matrix[0].length; 
            HashSet<Integer> zeroRow = new HashSet<>(); 
            HashSet<Integer> zeroCol = new HashSet<>(); 
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    if (matrix[i][j] == 0) {
                        zeroRow.add(i);
                        zeroCol.add(j);
                    }
                }
            }
            for (int i = 0; i < m; i++) {
                if (zeroRow.contains(i)) {
                    for (int j = 0; j < n; j++) matrix[i][j] = 0; 
                }
            }
            for (int j = 0; j < n; j++) {
                if (zeroCol.contains(j)) {
                    for (int i = 0; i < m; i++) matrix[i][j] = 0; 
                }
            }
            
            return;
        }
    }
    

  • 0
    S

    what is variable "move" used for? Thank you~


  • 0

    @superdtx oops it's unnecessary here plz ignore it


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.