¡GoConqr estrena diseño! Cuéntanos qué te parece.

Fichas

Set Matrix Zeroes

Suhas S Inglés 1 ficha

Fichas del conjunto (1)

  1. 1Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place. [ [1,1,1], [1,0,1], [1,1,1] ] Output: [ [1,0,1], [0,0,0], [1,0,1] ]public void setZeroes(int[][] matrix) {
    int rows = matrix.length;
    int cols = matrix[0].length;
    int col = 1;
    for( int i = 0; i < rows ; i++ ){
    if( matrix[i][0] == 0 ){
    col = 0;
    }
    for(int j = 1; j < cols ; j++ ){
    if( matrix[i][j] == 0 ){
    matrix[i][0] = 0;
    matrix[0][j] = 0;
    }
    }
    }
    for( int i = rows - 1; i >=0; i-- ){
    for( int j = cols - 1; j >=1; j-- ){
    if( matrix[i][0] == 0 || matrix[0][j] == 0 ){
    matrix[i][j] = 0;
    }
    }
    if( col == 0 ){
    matrix[i][0] = 0;
    }
    }
    }

Información

Descripción
Fichas sobre Set Matrix Zeroes, creado por Suhas S el 01/06/2018.
Creado por
Suhas S · jun 2018
Actualizado
1 jun 2018
Visitas
1
Valoración

Escríbenos

¿Sobre qué es tu mensaje?

¿Qué te parece el nuevo diseño?

¿Te gusta?