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. 用了 10 多年的 Tomcat 居然有bug !

    Java技术栈 www.javastack.cn 优秀的Java技术公众号 为了解决分布式链路追踪的问题,我们引入了实现OpenTracing的Jaeger来实现.然后我们为SpringBoot框架写 ...

  2. Phone List POJ-3630 字典树 or 暴力

    Phone List POJ-3630 字典树 or 暴力 题意 目前有 t 组数据, n 个电话号码,如果拨打号码的时候 先拨通了某个号码,那么这一串号码就无法全部拨通. 举个例子 911 和 91 ...

  3. web前后端数据交互

    前后端数据交互是每一名web程序员必须熟悉的过程,前后端的数据交互重点在于前端是如何获取后端返回的数据,毕竟后端一般情况下只需要将数据封装到一个jsonMap,然后return就完了.下面通过一个li ...

  4. SpringMVC拦截器(资源和权限管理)

    转自:https://www.cnblogs.com/downey/p/4928951.html 1.DispatcherServlet SpringMVC具有统一的入口DispatcherServl ...

  5. python-xss攻击和单例模式

    1.python中单例模式 class Foo: instance = None def __new__(cls, *args, **kwargs): if not Foo.instance: Foo ...

  6. django基础篇02-url路由系统

    django的路由系统: 一.基本用法: 1.path('index', views.index), # 通过类的方式创建url映射 2.path('home', views.Home.as_view ...

  7. Apache 安装后启动出现的错误

    错误信息 1: configure: error: APR not found 解决方法:yum install apr* -y 错误信息 2:httpd: apr_sockaddr_info_get ...

  8. MOVE - 重定位一个游标

    SYNOPSIS MOVE [ direction { FROM | IN } ] cursorname DESCRIPTION 描述 MOVE 在不检索数据的情况下重新定位一个游标. MOVE AL ...

  9. 35C3 CTF - Web - php

    参考链接 https://ctftime.org/task/7404 题目描述 PHP's unserialization mechanism can be exceptional. 解题流程 程序代 ...

  10. python3-多重继承

    继承是面向对象编程的一个重要的方式,因为通过继承,子类就可以扩展父类的功能. 回忆一下Animal类层次的设计,假设我们要实现以下4种动物: Dog - 狗狗: Bat - 蝙蝠: Parrot - ...