Solution 1: O(MN)
class Solution {
public void setZeroes(int[][] matrix) {
int[][] newMatrix=new int[matrix.length][matrix[0].length];
for(int i=0;i<matrix.length;i++){
for(int j=0;j<matrix[0].length;j++){
newMatrix[i][j]=matrix[i][j];
}
}
for(int i=0;i<matrix.length;i++){
for(int j=0;j<matrix[0].length;j++){
if(matrix[i][j]==0){
setZ(matrix,newMatrix,i,j);
}
}
}
for(int i=0;i<matrix.length;i++){
for(int j=0;j<matrix[0].length;j++){
matrix[i][j]=newMatrix[i][j];
}
}
}
public void setZ(int[][] matrix,int[][] newMatrix,int r,int c){
for(int i=0;i<matrix[0].length;i++){
newMatrix[r][i]=0;
}
for(int i=0;i<matrix.length;i++){
newMatrix[i][c]=0;
}
}
}
Solution 2: O(1)
用另一个数来代替0
但是这个方法实际是不对的,因为总会有case正好是你设的那个marker
class Solution {
public void setZeroes(int[][] matrix) {
for(int i=0;i<matrix.length;i++){
for(int j=0;j<matrix[0].length;j++){
if(matrix[i][j]==0){
for(int m=0;m<matrix[0].length;m++){
if(matrix[i][m]!=0)
matrix[i][m]=Integer.MIN_VALUE;
}
for(int k=0;k<matrix.length;k++){
if(matrix[k][j]!=0)
matrix[k][j]=Integer.MIN_VALUE;
}
}
}
}
for(int i=0;i<matrix.length;i++){
for(int j=0;j<matrix[0].length;j++){
if(matrix[i][j]==Integer.MIN_VALUE)
matrix[i][j]=0;
}
}
}
}
Solution 3: O(1), 只用了两个space
class Solution {
public void setZeroes(int[][] matrix) {
int R = matrix.length;
int C = matrix[0].length;
Boolean fr=false,fc=false;
//先标记第0行和第0列是否需要update
for(int i=0;i<C;i++){
if(matrix[0][i]==0)fr=true;
}
for(int j=0;j<R;j++){
if(matrix[j][0]==0)fc=true;
}
//扫描,更新标记
for (int i=1;i<R;i++){
for(int j=1;j<C;j++){
if(matrix[i][j]==0){
matrix[i][0]=0;
matrix[0][j]=0;
}
}
}
//根据标记来update
for(int i=1;i<R;i++){
for(int j=1;j<C;j++){
if(matrix[i][0]==0||matrix[0][j]==0){
matrix[i][j]=0;
}
}
}
//最后处理作为标记的第0行和第0列
if(fr){
for(int i=0;i<C;i++)matrix[0][i]=0;
}
if(fc){
for(int i=0;i<R;i++)matrix[i][0]=0;
}
}
}
Solution 4:O(1),只用了两个space,感觉没有3好想
第0行和第0列是做标记,从第一列开始扫描。
只要标记为0,那么就update所有除第0行和第0列的数字。
最后因为第0行和第0列被用作标记了,所以要特殊处理。
只要有行的第一个是0,那么isCol就是true,整个第一列要update。
只要第0行的列里面有0,就一定会把matrix[0][0]设0
所以只要两个空间。
class Solution {
public void setZeroes(int[][] matrix) {
Boolean isCol = false;
int R = matrix.length;
int C = matrix[0].length;
for (int i = 0; i < R; i++) {
// Since first cell for both first row and first column is the same i.e. matrix[0][0]
// We can use an additional variable for either the first row/column.
// For this solution we are using an additional variable for the first column
// and using matrix[0][0] for the first row.
if (matrix[i][0] == 0) {
isCol = true;
}
for (int j = 1; j < C; j++) {
// If an element is zero, we set the first element of the corresponding row and column to 0
if (matrix[i][j] == 0) {
matrix[0][j] = 0;
matrix[i][0] = 0;
}
}
}
// Iterate over the array once again and using the first row and first column, update the elements.
for (int i = 1; i < R; i++) {
for (int j = 1; j < C; j++) {
if (matrix[i][0] == 0 || matrix[0][j] == 0) {
matrix[i][j] = 0;
}
}
}
// See if the first row needs to be set to zero as well
if (matrix[0][0] == 0) {
for (int j = 0; j < C; j++) {
matrix[0][j] = 0;
}
}
// See if the first column needs to be set to zero as well
if (isCol) {
for (int i = 0; i < R; i++) {
matrix[i][0] = 0;
}
}
}
}