LeetCode073 Set Matrix Zeroes

详细见:leetcode.com/problems/set-matrix-zeroes


Java Solution: github

package leetcode;


public class P073_SetMatrixZeroes {
	public static void main(String[] args) {
		int[][] m = new int[][]{
			{1, 2, 0, 4},
			{1, 2, 3, 4},
			{1, 0, 3, 0},
			{1, 2, 3, 4},
		};
		new Solution().setZeroes(m);
		tools.Utils.A_打印二维数组(m);
	}
	/*
	 * 	一次AC
	 * 	3 ms
	 */
	static class Solution {
	    public void setZeroes(int[][] matrix) {
	        if (matrix == null || matrix.length == 0 || matrix[0].length == 0)
	        	return;
	        int i_save = 0, j_save = 0;
	        boolean isDone = false;
	        for (i_save = 0; i_save != matrix.length; i_save ++) {
	        	for (j_save = 0; j_save != matrix[0].length; j_save ++)
	        		if (matrix[i_save][j_save] == 0) {
	        			isDone = true;
	        			break;
	        		}
	        	if (isDone)	break;
	        }
	        if (! isDone)
	        	return;
	        for (int i = i_save; i != matrix.length; i ++)
	        	for (int j = 0; j != matrix[0].length; j ++)
	        		if (matrix[i][j] == 0) {
	        			matrix[i_save][j] = 0;
	        			matrix[i][j_save] = 0;
	        		}
	        for (int i = matrix.length - 1; i > -1; i --) {
	        	if (i == i_save)	continue;
	        	for (int j = matrix[0].length - 1; j > - 1; j --) {
	        		if (j == j_save)	continue;
	        		if (matrix[i_save][j] == 0 || matrix[i][j_save] == 0)
	        			matrix[i][j] = 0;
	        	}
	        }
	        for (int i = 0; i != matrix.length; i ++)
	        	matrix[i][j_save] = 0;
	        for (int j = 0; j != matrix[0].length; j ++)
	        	matrix[i_save][j] = 0;
	    }
	}
}


C Solution: github

/*
    url: leetcode.com/problems/set-matrix-zeroes
    AC 23ms 40.54%
*/

#include <stdio.h>
#include <stdlib.h>

void setZeroes(int** m, int xn, int yn) {
    int xi = 0, yi = 0, x0 = 1, y0 = 1;
    int x = 0, y = 0;
    for (xi = 0; xi < xn; xi ++)
        if (m[xi][0] == 0) y0 = 0;
    for (yi = 0; yi < yn; yi ++)
        if (m[0][yi] == 0) x0 = 0;

    printf("%d %d\r\n", x0, y0);
    for (xi = 1; xi < xn; xi ++) {
        for (yi = 1; yi < yn; yi ++) {
            if (m[xi][yi] == 0) {
                m[xi][0] = 0;
                m[0][yi] = 0;
            }
        }
    }
    for (xi = 1; xi < xn; xi ++) {
        for (yi = 1; yi < yn; yi ++) {
            if (m[xi][0] == 0 || m[0][yi] == 0) {
                m[xi][yi] = 0;
            }
        }
    }
    if (x0 == 0)
        for (yi = 0; yi < yn; yi ++)
            m[0][yi] = 0;
    if (y0 == 0)
        for (xi = 0; xi < xn; xi ++)
            m[xi][0] = 0;
}

int main() {
    int** m = (int**) malloc(sizeof(int*) * 2);
    int m0[] = {1, 1, 1};
    int m1[] = {0, 1, 2};
    int xn = 2, xi = 0;
    int yn = 3, yi = 0;
    m[0] = m0;
    m[1] = m1;
    setZeroes(m , 2, 3);
    for (xi = 0; xi < xn; xi ++) {
        for (yi = 0; yi < yn; yi ++)
            printf("%d ", m[xi][yi]);
        printf("\r\n");
    }
    free(m);
    return 0;
}


Python Solution: github

#coding=utf-8

'''
    url: leetcode.com/problems/set-matrix-zeroes/
    @author:     zxwtry
    @email:      zxwtry@qq.com
    @date:       2017年4月17日
    @details:    Solution: 182ms 36.33%
'''

class Solution(object):
    def setZeroes(self, m):
        """
        :type m: List[List[int]]
        :rtype: void Do not return anything, modify m in-place instead.
        """
        if m == None or len(m) == 0: return
        if m[0] == None or len(m[0]) == 0: return
        rn, cn = len(m), len(m[0])
        rs, cs = False, False
        for ri in range(rn):
            rs = rs or (m[ri][0] == 0)
        for ci in range(cn):
            cs = cs or (m[0][ci] == 0)
        for ri in range(1, rn):
            for ci in range(1, cn):
                if m[ri][ci] == 0:
                    m[0][ci] = 0
                    m[ri][0] = 0
        for ri in range(1, rn):
            for ci in range(1, cn):
                if m[0][ci] == 0 or m[ri][0] == 0:
                    m[ri][ci] = 0
        if rs:
            for ri in range(rn):
                m[ri][0] = 0
        if cs:
            for ci in range(cn):
                m[0][ci] = 0
                

if __name__ == "__main__":
    m = [[1, 2, 3], [0, 4, 5]]
    Solution().setZeroes(m)
    print(m)
        


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值