LeetCode073 Set Matrix Zeroes
来源:互联网 发布:淘宝手机详情页宽度 编辑:程序博客网 时间:2024/06/08 16:01
详细见: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
- LeetCode073 Set Matrix Zeroes
- LeetCode: Set Matrix Zeroes
- Set Matrix Zeroes
- Set Matrix Zeroes
- LeetCode Set Matrix Zeroes
- [Leetcode] Set Matrix Zeroes
- LeetCode: Set Matrix Zeroes
- Set Matrix Zeroes
- Leetcode: Set Matrix Zeroes
- [LeetCode] Set Matrix Zeroes
- Set Matrix Zeroes
- Set Matrix Zeroes
- [LeetCode]Set Matrix Zeroes
- Set Matrix Zeroes
- Set Matrix Zeroes
- Set Matrix Zeroes
- [leetcode]Set Matrix Zeroes
- Leetcode:Set Matrix Zeroes
- 插入排序&希尔排序—思路及实现
- 使用阻塞队列实现生产者-消费者模式——Java实现
- 一篇SSM框架整合友好的文章(二)
- 输出以下的杨辉三角形
- Tomcat servlet工作原理
- LeetCode073 Set Matrix Zeroes
- manifest文件的常用配置项
- 动态规划 24题
- 修饰符
- LeetCode074 Search a 2D Matrix
- StringHelper--封转自己的字符串工具类
- Spring框架介绍和IoC容器中Bean配置(Spring框架)
- ICMP报文和ping命令
- 一些JavaScript小技巧