leetcode-mid-array-73 set matrix zeros
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的更多相关文章
- 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 ...
- [LeetCode] 74 Search a 2D Matrix(二分查找)
二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...
- [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 ...
- 【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 ...
- 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 ...
- C++ STL@ list 应用 (leetcode: Rotate Array)
STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...
- Leetcode 240 Search a 2D Matrix II (二分法和分治法解决有序二维数组查找)
1.问题描写叙述 写一个高效的算法.从一个m×n的整数矩阵中查找出给定的值,矩阵具有例如以下特点: 每一行从左到右递增. 每一列从上到下递增. 2. 方法与思路 2.1 二分查找法 依据矩阵的特征非常 ...
- [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 ...
- leetcode 数组array
120. Triangle 给出一个三角形(数据数组),找出从上往下的最小路径和.每一步只能移动到下一行中的相邻结点上. 解法,自底向上 The idea is simple. Go from bot ...
- 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 ...
随机推荐
- 剑指offer-删除链表中重复的结点-链表-python ***
题目描述 在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针. 例如,链表1->2->3->3->4->4->5 处理后 ...
- spring整合apache-shiro的简单使用
这里不对shiro进行介绍,介绍什么的没有意义 初学初用,不求甚解,简单使用 一.导入jar包(引入坐标) <!--shiro和spring整合--> <dependency> ...
- Android--ViewPager点击按钮切换下一页
不再重写一遍了,看csdn: https://blog.csdn.net/qq_42866164/article/details/101346058
- nmbd - 向客户端提供构造在IP之上的NetBIOS名字服务的NetBIOS名字服务器
总览 SYNOPSIS nmbd [-D] [-F] [-S] [-a] [-i] [-o] [-h] [-V][-d <debug level>] [-H <lmhosts fil ...
- 将数据转为tfrecord格式
假设emo文件夹下,有1,2,3,4等文件夹,每个文件夹代表一个类别 import tensorflow as tf from PIL import Image from glob import gl ...
- flutter 布局简介
import 'package:flutter/material.dart'; class LayoutDemo extends StatelessWidget { @override Widget ...
- pandas 的axis参数的理解
# pandas的axis参数怎样理解? # axis=0 或者 "index": # 如果是单行操作,就指的是某一行 # 如果是聚合操作,指的是跨行cross rows # ax ...
- git分支管理与tag的学习笔记
git分支管理学习笔记:创建dev分支:git branch dev查看分支:git branch切换分支:git checkout dev创建并切换分支:git checkout dev -b zh ...
- 在父组件中,传值给子组件-vue
1.通过 props <x-test :name="username"></x-test>1)props为字符串数组 props: ['name']2)pr ...
- IO操作基本步骤
package com.study02; import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundE ...