leetcode Set Matrix Zeroes
2015-11-16 16:14
323 查看
Question
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.Solution
If you have to store some thing, but the problem requires in place solution, you may consider storing it in the array given.class Solution(object): def setZeroes(self, matrix): """ :type matrix: List[List[int]] :rtype: void Do not return anything, modify matrix in-place instead. """ if matrix == None or len(matrix) == 0: return first_col = False first_row = False for i in xrange(len(matrix[0])): if matrix[0][i] == 0: first_row = True break; for i in xrange(len(matrix)): if matrix[i][0] == 0: first_col = True break; for i in xrange(1,len(matrix)): for j in xrange(1,len(matrix[0])): if matrix[i][j] == 0: matrix[i][0] = 0 matrix[0][j] = 0 for i in xrange(1,len(matrix)): for j in xrange(1,len(matrix[0])): if matrix[i][0] == 0 or matrix[0][j] == 0: matrix[i][j] = 0 if first_row: for i in xrange(len(matrix[0])): matrix[0][i] = 0 if first_col: for i in xrange(len(matrix)): matrix[i][0] = 0
相关文章推荐
- C++学习网站
- 转: CSS中float和clear的理解
- (一)那一天
- Java内功提升之关键字
- display tag 标签分页实例
- vim使用小结
- html5 Up and Running学习笔记(一)
- Vector方法magnitude
- bool变量与零值的比较
- Qt封装SQLite的操作
- [CodeChef OCT13]斐波那契数Fibonacci Number解题报告
- 绪论 – 数据结构和算法01
- IOS 解决.dylib找不到的问题
- 关于如何在项目启动时加载配置文件信息
- fir.im Weekly - iOS 保持界面流畅的技巧
- 一个好前端要会什么
- 【Leetcode-easy】Reverse Integer
- 7.首页、bitmaputils
- 如何在android style文件中使用自定义属性
- 利用FS寄存器得到任意函数地址