Follow up:
Did you use extra space?
A straight forward solution using O(mn) space is probably a bad idea.
A simple improvement uses O(m + n) space, but still not the best solution.
Could you devise a constant space solution?
public class Solution {
public void setZeroes(int[][] matrix) {
if(matrix==null) {
return;
}
int m = matrix.length;
if(m==0) {
return;
}
int n = matrix[0].length;
HashSet<Integer> rows = new HashSet<Integer>();
HashSet<Integer> columns = new HashSet<Integer>();
for(int i=0;i<m;i++) {
for(int j=0;j<n;j++) {
int ma = matrix[i][j];
if(ma==0) {
rows.add(i);
columns.add(j);
}
}
}
for(int row : rows) {
for(int j=0;j<n;j++) {
matrix[row][j] = 0;
}
}
for(int column : columns) {
for(int i=0;i<m;i++) {
matrix[i][column] = 0;
}
}
}
}
public class Solution {
public void setZeroes(int[][] matrix) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return;
}
int row = matrix.length;
int column = matrix[0].length;
boolean isFirstRowZero = false;
boolean isFirstColumnZero = false;
for (int i = 0; i < column; i++) {
if (matrix[0][i] == 0) {
isFirstRowZero = true;
}
}
for (int j = 0; j < row; j++) {
if (matrix[j][0] == 0) {
isFirstColumnZero = true;
}
}
for (int i = 1; i < row; i++) {
for (int j = 1; j < column; j++) {
if (matrix[i][j] == 0) {
matrix[i][0] = 0;
matrix[0][j] = 0;
}
}
}
for (int i = 1; i < column; i++) {
if (matrix[0][i] == 0) {
for (int j = 1; j < row; j++) {
matrix[j][i] = 0;
}
}
}
for (int i = 1; i < row; i++) {
if (matrix[i][0] == 0) {
for (int j = 1; j < column; j++) {
matrix[i][j] = 0;
}
}
}
if (isFirstRowZero) {
for (int i = 0; i < column; i++) {
matrix[0][i] = 0;
}
}
if (isFirstColumnZero) {
for (int i = 0; i < row; i++) {
matrix[i][0] = 0;
}
}
}
}
public class Solution {
public void setZeroes(int[][] matrix) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return;
}
int row = matrix.length;
int column = matrix[0].length;
boolean isFirstRowZero = false;
boolean isFirstColumnZero = false;
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
if (matrix[i][j] == 0) {
if (i == 0) {
isFirstRowZero = true;
}
if (j == 0) {
isFirstColumnZero = true;
}
matrix[i][0] = 0;
matrix[0][j] = 0;
}
}
}
for (int i = 1; i < row; i++) {
for (int j = 1; j < column; j++) {
if (matrix[0][j] == 0 || matrix[i][0] == 0) {
matrix[i][j] = 0;
}
}
}
if (isFirstRowZero) {
for (int i = 0; i < column; i++) {
matrix[0][i] = 0;
}
}
if (isFirstColumnZero) {
for (int i = 0; i < row; i++) {
matrix[i][0] = 0;
}
}
}
}
A straight forward solution using O(mn) space is probably a bad idea.
A simple improvement uses O(m + n) space, but still not the best solution.
Could you devise a constant space solution?
public class Solution {
public void setZeroes(int[][] matrix) {
if(matrix==null) {
return;
}
int m = matrix.length;
if(m==0) {
return;
}
int n = matrix[0].length;
HashSet<Integer> rows = new HashSet<Integer>();
HashSet<Integer> columns = new HashSet<Integer>();
for(int i=0;i<m;i++) {
for(int j=0;j<n;j++) {
int ma = matrix[i][j];
if(ma==0) {
rows.add(i);
columns.add(j);
}
}
}
for(int row : rows) {
for(int j=0;j<n;j++) {
matrix[row][j] = 0;
}
}
for(int column : columns) {
for(int i=0;i<m;i++) {
matrix[i][column] = 0;
}
}
}
}
public class Solution {
public void setZeroes(int[][] matrix) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return;
}
int row = matrix.length;
int column = matrix[0].length;
boolean isFirstRowZero = false;
boolean isFirstColumnZero = false;
for (int i = 0; i < column; i++) {
if (matrix[0][i] == 0) {
isFirstRowZero = true;
}
}
for (int j = 0; j < row; j++) {
if (matrix[j][0] == 0) {
isFirstColumnZero = true;
}
}
for (int i = 1; i < row; i++) {
for (int j = 1; j < column; j++) {
if (matrix[i][j] == 0) {
matrix[i][0] = 0;
matrix[0][j] = 0;
}
}
}
for (int i = 1; i < column; i++) {
if (matrix[0][i] == 0) {
for (int j = 1; j < row; j++) {
matrix[j][i] = 0;
}
}
}
for (int i = 1; i < row; i++) {
if (matrix[i][0] == 0) {
for (int j = 1; j < column; j++) {
matrix[i][j] = 0;
}
}
}
if (isFirstRowZero) {
for (int i = 0; i < column; i++) {
matrix[0][i] = 0;
}
}
if (isFirstColumnZero) {
for (int i = 0; i < row; i++) {
matrix[i][0] = 0;
}
}
}
}
public class Solution {
public void setZeroes(int[][] matrix) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return;
}
int row = matrix.length;
int column = matrix[0].length;
boolean isFirstRowZero = false;
boolean isFirstColumnZero = false;
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
if (matrix[i][j] == 0) {
if (i == 0) {
isFirstRowZero = true;
}
if (j == 0) {
isFirstColumnZero = true;
}
matrix[i][0] = 0;
matrix[0][j] = 0;
}
}
}
for (int i = 1; i < row; i++) {
for (int j = 1; j < column; j++) {
if (matrix[0][j] == 0 || matrix[i][0] == 0) {
matrix[i][j] = 0;
}
}
}
if (isFirstRowZero) {
for (int i = 0; i < column; i++) {
matrix[0][i] = 0;
}
}
if (isFirstColumnZero) {
for (int i = 0; i < row; i++) {
matrix[i][0] = 0;
}
}
}
}
没有评论:
发表评论