leetcode Set Matrix Zeroes

leetcode Set Matrix Zeroes

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

Follow up:Did you use extra space?
A straight forward solution using O(mn) space is probably a bad idea.
A simple improvement uses O(m + n) space, but still not the best solution.
Could you devise a constant space solution?

题目地址:leetcode Set Matrix Zeroes

题目大意: 给定一个矩阵,如果它其中的某个元素为0,那么该元素所在行和所在列均变为0.

思路:

  1. 创建一个矩阵的拷贝,然后根据这个拷贝进行判断O(MN)
  2. 创建一个数组,记录矩阵为0的行和列下标O(m+n)
  3. 把有0的元素映射到首行和首列

 

Python 方法1

 

 

Python 方法2

 

Python 方法3

 

C++方法3

 

本博客若无特殊说明则由 hrwhisper 原创发布
转载请点名出处:细语呢喃 > leetcode Set Matrix Zeroes
本文地址:https://www.hrwhisper.me/leetcode-set-matrix-zeroes/

打赏一杯咖啡钱呗

Leetcode . permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *