Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
zeroMatrix
print(zeroMatrix([[0,2,0], [4,5,6],[7,8,9]])); // if an element is 0 -> row and column are set to 0. /* [ 0 2 0 ] [ 0 0 0 ] [ 4 5 6 ] -> [ 0 5 0 ] [ 7 8 9 ] [ 0 6 0 ] */ function zeroMatrix(matrix) { if (!matrix) { throw new Error('invalid matrix'); } if (matrix.length === 0 || matrix.length === 1) { return matrix; } let rowsLen = matrix.length; let colsLen = matrix[0].length; let rows = new Object(); let cols = new Object(); for (let y = 0; y < rowsLen; ++y) { for (let x = 0; x < colsLen; ++x) { if (matrix[y][x] === 0) { rows[y] = true; cols[x] = true; } } } for (let y = 0; y < rows.length; ++y) { for (let x = 0; x < cols.length; ++x) { if (rows.hasOwnProperty(y) || cols.hasOwnProperty(x)) { matrix[y][x] = 0; } } } return matrix; }
run
|
edit
|
history
|
help
0
TaskhelTemplate
G
#include
Futbal
Looping Strings
my dick!!!
Strange Attractors
G
Just stop thinking about it and go eat
Prime Factors