73 Set Matrix Zeroes 矩陣置零
Description:
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place.
Example:
Example 1:
Input:
[
[1,1,1],
[1,0,1],
[1,1,1]
]
Output:
[
[1,0,1],
[0,0,0],
[1,0,1]
]
Example 2:
Input:
[
[0,1,2,0],
[3,4,5,2],
[1,3,1,5]
]
Output:
[
[0,0,0,0],
[0,4,5,0],
[0,3,1,0]
]
Follow up:
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?
題目描述:
給定一個 m x n 的矩陣艰毒,如果一個元素為 0雇毫,則將其所在行和列的所有元素都設為 0纳击。請使用原地算法斗埂。
示例 :
示例 1:
輸入:
[
[1,1,1],
[1,0,1],
[1,1,1]
]
輸出:
[
[1,0,1],
[0,0,0],
[1,0,1]
]
示例 2:
輸入:
[
[0,1,2,0],
[3,4,5,2],
[1,3,1,5]
]
輸出:
[
[0,0,0,0],
[0,4,5,0],
[0,3,1,0]
]
進階:
一個直接的解決方案是使用 O(mn) 的額外空間早龟,但這并不是一個好的解決方案坯墨。
一個簡單的改進方案是使用 O(m + n) 的額外空間芥喇,但這仍然不是最好的解決方案使套。
你能想出一個常數(shù)空間的解決方案嗎罐呼?
思路:
將矩陣的首行和首列用來記錄該行/列是否需要全部置 0
需要額外的兩個 bool變量記錄首行和首列是否需要置 0
- 遍歷首行/列, 記錄是否需要置 0
- 遍歷矩陣, 如果需要置 0, 將首行/列第一個元素置 0
- 遍歷矩陣, 將需要置 0的行/列置 0
- 將首行/列置 0
時間復雜度O(mn), 空間復雜度O(1)
代碼:
C++:
class Solution
{
public:
void setZeroes(vector<vector<int>>& matrix)
{
bool row = false, col = false;
for (int i = 0; i < matrix.size(); i++)
{
if (matrix[i][0] == 0)
{
col = true;
break;
}
}
for (int i = 0; i < matrix[0].size(); i++)
{
if (matrix[0][i] == 0)
{
row = true;
break;
}
}
for (int i = 0; i < matrix.size(); i++)
{
for (int j = 0; j < matrix[0].size(); j++)
{
if (matrix[i][j] == 0)
{
matrix[i][0] = 0;
matrix[0][j] = 0;
}
}
}
for (int i = 1; i < matrix.size(); i++) for (int j = 1; j < matrix[0].size(); j++) if (matrix[i][0] == 0 or matrix[0][j] == 0) matrix[i][j] = 0;
if (row) for (int i = 0; i < matrix[0].size(); i++) matrix[0][i] = 0;
if (col) for (int i = 0; i < matrix.size(); i++) matrix[i][0] = 0;
}
};
Java:
class Solution {
public void setZeroes(int[][] matrix) {
boolean row = false, col = false;
for (int i = 0; i < matrix.length; i++) {
if (matrix[i][0] == 0) {
col = true;
break;
}
}
for (int i = 0; i < matrix[0].length; i++) {
if (matrix[0][i] == 0) {
row = true;
break;
}
}
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
if (matrix[i][j] == 0) {
matrix[i][0] = 0;
matrix[0][j] = 0;
}
}
}
for (int i = 1; i < matrix.length; i++) for (int j = 1; j < matrix[0].length; j++) if (matrix[i][0] == 0 || matrix[0][j] == 0) matrix[i][j] = 0;
if (row) for (int i = 0; i < matrix[0].length; i++) matrix[0][i] = 0;
if (col) for (int i = 0; i < matrix.length; i++) matrix[i][0] = 0;
}
}
Python:
class Solution:
def setZeroes(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
row, col = False, False
for i in range(len(matrix)):
if not matrix[i][0]:
col = True
break
for i in range(len(matrix[0])):
if not matrix[0][i]:
row = True
break
for i in range(len(matrix)):
for j in range(len(matrix[0])):
if not matrix[i][j]:
matrix[i][0] = 0
matrix[0][j] = 0
for i in range(1, len(matrix)):
for j in range(1, len(matrix[0])):
if not matrix[i][0] or not matrix[0][j]:
matrix[i][j] = 0
if row:
for i in range(len(matrix[0])):
matrix[0][i] = 0
if col:
for i in range(len(matrix)):
matrix[i][0] = 0