JAVA O(1)


  • 0
    O

    public class Solution {
    public void setZeroes(int[][] matrix) {
    if(matrix.length == 0 || matrix.length == 0){

        }else{
            setWorker(matrix);
        }
    }
    private void setWorker(int[][] matrix){
        int setRow = -1;
        //int flag = false;
        for(int i=0;i<matrix.length;i++){
            boolean flag = false;
            for(int j=0;j<matrix[0].length;j++){
                if(matrix[i][j]==0){
                    if(setRow != -1){
                        matrix[setRow][j] = 0;
                        flag =true;
                    }else{
                        setRow = i;
                        break;
                    }
                }
            }
            if(flag){
                for(int j=0;j<matrix[0].length;j++){
                    matrix[i][j]=0;
                }
            }
        }
        
        if(setRow != -1){
            for(int j=0;j<matrix[0].length;j++){
                if(matrix[setRow][j] == 0){
                    for(int i=0;i<matrix.length;i++){
                        matrix[i][j] = 0;
                    }
                }
                matrix[setRow][j] = 0;
            }
        }
    }
    

    }


Log in to reply
 

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