Skip to content

Commit

Permalink
Ruby Solution for Set Matrix Zeroes
Browse files Browse the repository at this point in the history
  • Loading branch information
saip7795 committed Jan 12, 2023
1 parent eff3535 commit 7843167
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions ruby/0073-set-matrix-zeroes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
def set_zeroes(matrix)

rows = matrix.length - 1
cols = matrix[0].length - 1
row_zero = false


(0..rows).each do |r|
(0..cols).each do |c|
if(matrix[r][c] == 0)
matrix[0][c] = 0
if r > 0
matrix[r][0] = 0
else
row_zero = true
end
end
end
end

(1...rows+1).each do |r|
(1..cols).each do |c|
matrix[r][c] = 0 if ((matrix[0][c] == 0)||(matrix[r][0] == 0))
end
end

if matrix[0][0] == 0
(0..rows).each do |r|
matrix[r][0] = 0
end
end

if row_zero
(0..cols).each do |c|
matrix[0][c] = 0
end
end
end

0 comments on commit 7843167

Please sign in to comment.