Leetcode 73. Set Matrix Zeroes
文章作者:Tyan
部落格:noahsnail.com | CSDN | 簡書
1. Description
2. Solution
- Version 1
class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
int rows = matrix.size();
if(rows == 0) {
return;
}
int columns = matrix[0].size();
vector<int> row;
vector<int> column;
for(int i = 0; i < rows; i++) {
for(int j = 0; j < columns; j++) {
if(!matrix[i][j]) {
row.push_back(i);
column.push_back(j);
}
}
}
for(int i = 0; i < row.size(); i++) {
for(int j = 0; j < columns; j++) {
matrix[row[i]][j] = 0;
}
}
for(int j = 0; j < column.size(); j++) {
for(int i = 0; i < rows; i++) {
matrix[i][column[j]] = 0;
}
}
}
};
- Version 2
class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
int rows = matrix.size();
if(rows == 0) {
return;
}
int columns = matrix[0].size();
bool row = false;
bool column = false;
for(int i = 0; i < rows; i++) {
for(int j = 0; j < columns; j++) {
if(!matrix[i][j]) {
if(!i) {
row = true;
}
if(!j) {
column = true;
}
matrix[0][j] = 0;
matrix[i][0] = 0;
}
}
}
for(int i = 1; i < rows; i++) {
for(int j = 1; j < columns; j++) {
if(!matrix[0][j] || !matrix[i][0]) {
matrix[i][j] = 0;
}
}
}
if(row) {
for(int j = 0; j < columns; j++) {
matrix[0][j] = 0;
}
}
if(column) {
for(int i = 0; i < rows; i++) {
matrix[i][0] = 0;
}
}
}
};
Reference
相關文章
- Leetcode Set Matrix ZeroesLeetCode
- LeetCode 73 Set Matrix ZeroesLeetCode
- Leetcode-Set Matrix ZeroesLeetCode
- Set Matrix Zeroes leetcode javaLeetCodeJava
- leetcode學習筆記73 Set Matrix ZeroesLeetCode筆記
- 演算法Set Matrix Zeroes演算法
- [CareerCup] 1.7 Set Matrix Zeroes 矩陣賦零矩陣
- LeetCode—283—Move ZeroesLeetCode
- Leetcode Spiral MatrixLeetCode
- 283. Move Zeroes--LeetCode RecordLeetCode
- Leetcode 54 Spiral MatrixLeetCode
- Leetcode Spiral Matrix IILeetCode
- Leetcode-Spiral MatrixLeetCode
- Spiral Matrix leetcode javaLeetCodeJava
- ZOJ Problem Set - 1094 Matrix Chain MultiplicationAI
- LeetCode 542. 01 MatrixLeetCode
- [LeetCode] 867. Transpose MatrixLeetCode
- LeetCode-Sparse Matrix MultiplicationLeetCode
- LeetCode 59 Spiral Matrix IILeetCode
- Leetcode-Spiral Matrix IILeetCode
- Spiral Matrix II leetcode javaLeetCodeJava
- Leetcode 172. Factorial Trailing ZeroesLeetCodeAI
- leetcode刷題--Factorial Trailing ZeroesLeetCodeAI
- Leetcode Search a 2D MatrixLeetCode
- LeetCode-Longest Increasing Path in a MatrixLeetCode
- Leetcode-Search a 2D MatrixLeetCode
- Search a 2D Matrix leetcode javaLeetCodeJava
- Leetcode: 627 - UPDATE, SET, ifLeetCode
- Leetcode 329. Longest Increasing Path in a MatrixLeetCode
- LeetCode-Search a 2D Matrix IILeetCode
- LeetCode 240 Search a 2D Matrix IILeetCode
- LeetCode-Kth Smallest Element in a Sorted MatrixLeetCode
- [LeetCode] 2326. Spiral Matrix IVLeetCode
- 每天一道LeetCode--172. Factorial Trailing ZeroesLeetCodeAI
- [LeetCode] 378. Kth Smallest Element in a Sorted MatrixLeetCode
- 演算法練習--LeetCode--54. Spiral Matrix 100%演算法LeetCode
- Leetcode 329. Longest Increasing Path in a Matrix (python+cpp)LeetCodePython
- Matrix Computations