public class Solution { public void setZeroes(int[][] matrix) { int m = matrix.length; if (m == 0) return; int n = matrix[0].length; // Adopt hashtable to store coordinates of zeros Set<Integer> row = new HashSet<>(); Set<Integer> column = new HashSet<>(); // Store coordinates of zeros for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) if (matrix[i][j] == 0) { row.add(i); column.add(j); } // Set zeros for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) if (row.contains(i) || column.contains(j)) matrix[i][j] = 0; } }
Computer Vision & Image Processing & Machine Learning & Pattern Recognition
Thursday, January 8, 2015
LeetCode 73: Set Matrix Zeroes
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment