mycode

空间复杂度 m+n

思路:用set把为0元素所在行、列记录下来

注意:注释的方法更快

class Solution(object):
def setZeroes(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: None Do not return anything, modify matrix in-place instead.
"""
rows = set()
cols = set()
count_row = len(matrix)
count_col = len(matrix[0])
for i in range(count_row):
for j in range(count_col):
if matrix[i][j] == 0:
rows.add(i)
cols.add(j)
#for row in rows:
# matrix[row] = [0]*count_col
#for col in cols:
# for line in matrix:
# line[col] = 0
for i in rows:
for j in xrange(count_col):
matrix[i][j] = 0
for i in xrange(count_row):
for j in cols:
matrix[i][j] = 0
return matrix

参考:

常数空间,用第一行第一列来记录

class Solution(object):
def setZeroes(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: None Do not return anything, modify matrix in-place instead.
"""
first_row = False
first_col = False
m = len(matrix)
n = len(matrix[0]) #[[]]
if m == 0 or n == 0 :
return
flag = -1
for i in range(m):
if matrix[i][0] == 0: #说明第一列本来最后就应该都等于0,以免被覆盖
first_col = True
for j in range(n):
if matrix[0][j] == 0:
first_row = True
for i in range(1,m):
for j in range(1,n):
if matrix[i][j] == 0:
matrix[i][0] = matrix[0][j] = 0
#第一行所有列,第一列所有行 为对照表,所以节省了空间
for i in range(1,m):
for j in range(1,n):
if matrix[0][j] == 0 or matrix[i][0] == 0:
matrix[i][j] = 0
if first_row:
for j in range(n):
matrix[0][j] = 0
if first_col:
for i in range(m):
matrix[i][0] = 0

leetcode-mid-array-73 set matrix zeros的更多相关文章

  1. LeetCode 73. Set Matrix Zeros(矩阵赋零)

    Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. click ...

  2. [LeetCode] 74 Search a 2D Matrix(二分查找)

    二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...

  3. [LeetCode] 74. Search a 2D Matrix 搜索一个二维矩阵

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  4. 【leetcode】Search a 2D Matrix

    Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...

  5. Java for LeetCode 074 Search a 2D Matrix

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  6. C++ STL@ list 应用 (leetcode: Rotate Array)

    STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...

  7. Leetcode 240 Search a 2D Matrix II (二分法和分治法解决有序二维数组查找)

    1.问题描写叙述 写一个高效的算法.从一个m×n的整数矩阵中查找出给定的值,矩阵具有例如以下特点: 每一行从左到右递增. 每一列从上到下递增. 2. 方法与思路 2.1 二分查找法 依据矩阵的特征非常 ...

  8. [LeetCode&Python] Problem 766. Toeplitz Matrix

    A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element. Now given ...

  9. leetcode 数组array

    120. Triangle 给出一个三角形(数据数组),找出从上往下的最小路径和.每一步只能移动到下一行中的相邻结点上. 解法,自底向上 The idea is simple. Go from bot ...

  10. leetcode 【Search a 2D Matrix 】python 实现

    题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the f ...

随机推荐

  1. 解决跳转出现 No input file specified.

    项目根目录中.htaccess文件修改为: <IfModule mod_rewrite.c>  Options +FollowSymlinks  RewriteEngine On Rewr ...

  2. Centos7防火墙开启3306端口

    CentOS7的默认防火墙为firewall,且默认是不打开的. systemctl start firewalld # 启动friewall systemctl status firewalld # ...

  3. wordpress数据库结构以及数据表之间的关系

    默认WordPress一共有以下11个表.这里加上了默认的表前缀 wp_ . wp_commentmeta:存储评论的元数据 wp_comments:存储评论 wp_links:存储友情链接(Blog ...

  4. 关于原型链,原来这么简单?—————终结__proto__和prototype的那些事

    今天,一个技术群里小朋友提出一个问题: Object.prototype.a = function () { console.log('a') } Function.prototype.b = fun ...

  5. PowerEdge T630服务器安装机器学习环境(Ubuntu18.04、Nvidia 1080Ti驱动、CUDA及CUDNN安装)

    安装步骤 在开始安装之前,我要说明一下,这个Ubuntu18.04系统的安装,使用的连接线(就是服务器和电脑显示器的连接线)必须两头都是VGA连接线,不能使用VGA转HDMI连接线,也不能用DVI转D ...

  6. Python 自定义三方库

    一.注册一个pypi账号 https://pypi.org/ 二.github上创建一个项目 https://github.com/ 三.编写自己的python项目 项目结构(参考):https:// ...

  7. iOS开发之详解剪贴板

    关于UIMenuController的用法例子 今天终于搞明白了UIMenuController显示的相关内容,把源代码分享给大家! 要正常显示菜单,必须做到以下几点:1. -(BOOL)canBec ...

  8. index 索引

    1.创建表 drop table if exists kg_fk_user;create table kg_fk_user(id int,name string)row format delimite ...

  9. BSOJ5467 [CSPX2017#3]整数 莫比乌斯反演+杜教筛

    题意简述 给你两个整数\(n\),\(k\),让你求出这个式子 \[ \sum_{a_1=1}^n \sum_{a_2=a_1}^n \sum_{a_3=a_2}^n \cdots \sum_{a_k ...

  10. 模型监控指标- 混淆矩阵、ROC曲线,AUC值,KS曲线以及KS值、PSI值,Lift图,Gain图,KT值,迁移矩阵

    1. 混淆矩阵 确定截断点后,评价学习器性能 假设训练之初以及预测后,一个样本是正例还是反例是已经确定的,这个时候,样本应该有两个类别值,一个是真实的0/1,一个是预测的0/1 TP(实际为正预测为正 ...