Share my O(1) space fast & clean Java code


  • 0
    D
    public class Solution {
        public void setZeroes(int[][] matrix) {
            
            int m = -1;
            for(int i = 0; i<matrix.length; i++){
                boolean foundZero = false;
                for(int j=0; j<matrix[i].length; j++){
                    if(matrix[i][j] == 0){
                        foundZero = true;
                        break;
                    }
                }
                if(foundZero && m==-1){
                    m = i;
                    continue;
                }
                if(foundZero){
                    for(int j =0;j<matrix[i].length; j++){
                        if(matrix[i][j] == 0) matrix[m][j] = 0;
                        matrix[i][j] = 0;
                    }
                }
            }
            if(m!= -1){
                for(int j = 0; j<matrix[m].length; j++){
                    if(matrix[m][j] == 0)
                    for(int i =0; i<matrix.length; i++)
                        matrix[i][j] = 0;
                    matrix[m][j] = 0;
                }
            }
        }
    }

  • 1
    D

    too clean even without any comments...


Log in to reply
 

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