Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

set matrix zeros programming creek

public class Solution {
    public void setZeroes(int[][] matrix) {
        boolean firstRowZero = false;
        boolean firstColumnZero = false;
 
        //set first row and column zero or not
        for(int i=0; i<matrix.length; i++){
            if(matrix[i][0] == 0){
                firstColumnZero = true;
                break;
            }
        }
 
        for(int i=0; i<matrix[0].length; i++){
            if(matrix[0][i] == 0){
                firstRowZero = true;
                break;
            }
        }
 
        //mark zeros on first row and column
        for(int i=1; i<matrix.length; i++){
            for(int j=1; j<matrix[0].length; j++){
                if(matrix[i][j] == 0){
                   matrix[i][0] = 0;
                   matrix[0][j] = 0;
                }
            }
        }
 
        //use mark to set elements
        for(int i=1; i<matrix.length; i++){
            for(int j=1; j<matrix[0].length; j++){
                if(matrix[i][0] == 0 || matrix[0][j] == 0){
                   matrix[i][j] = 0;
                }
            }
        }
 
        //set first column and row
        if(firstColumnZero){
            for(int i=0; i<matrix.length; i++)
                matrix[i][0] = 0;
        }
 
        if(firstRowZero){
            for(int i=0; i<matrix[0].length; i++)
                matrix[0][i] = 0;
        }
 
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: Random Float in java 
Java :: findone in spring boot 2.4.1 
Java :: latest android sdk version 
Java :: local date to date java 
Java :: do you need java installed for kafka 
Java :: Syntax of how to create ArrayList in Java 
Java :: what are abstract methods in java 
Java :: java bigdecimal 
Java :: hashmap 
Java :: Java path to file 
Java :: do while jaca 
Java :: sorting collections with comparator java 
Java :: spiral traversal of matrix leetcode 
Java :: write content to file java 
Java :: Example of Creating a Java Stack 
Java :: Java How to use List? 
Java :: remove tableview separator lines in javafx 
Java :: app "restart" the home activity (and dismiss all other activities). 
Java :: java print boolean with spaces 
Java :: quicksort java 
Java :: transformer un string en Biginteger java 
Java :: Java Creating a Java LinkedList 
Java :: windows Jmeter java.net.BindException: Address already in use: connect 
Java :: read excel file in java and store into arraylist 
Java :: dependency injection java 
Java :: mod 10e9+7 in java 
Java :: java.lang.SecurityException: Permission Denial: reading androidx.core.content.FileProvider 
Java :: Java No-Arg Constructors 
Java :: next method jdbc 
Java :: android studio json parser 
ADD CONTENT
Topic
Content
Source link
Name
4+1 =