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
相關文章
- Set-matrix-zeroes
- leetcode學習筆記73 Set Matrix ZeroesLeetCode筆記
- LeetCode—283—Move ZeroesLeetCode
- Leetcode 54 Spiral MatrixLeetCode
- [LeetCode] 867. Transpose MatrixLeetCode
- LeetCode 542. 01 MatrixLeetCode
- [LeetCode] 2326. Spiral Matrix IVLeetCode
- Leetcode 329. Longest Increasing Path in a MatrixLeetCode
- Leetcode: 627 - UPDATE, SET, ifLeetCode
- [LeetCode] 378. Kth Smallest Element in a Sorted MatrixLeetCode
- Leetcode 329. Longest Increasing Path in a Matrix (python+cpp)LeetCodePython
- 演算法練習--LeetCode--54. Spiral Matrix 100%演算法LeetCode
- 474. Ones and Zeroes
- Matrix
- Matrix Computations
- Matrix Distance
- Matrix Operations
- Factorial Trailing Zeroes 階乘後的零AI
- Lintcode539 Move Zeroes solution 題解
- 6.lambda-matrix
- Spiral-matrix-ii
- D. Matrix Cascade
- 初探Matrix Android ApkCheckerAndroidAPK
- OpenGL Matrix Class (C++)C++
- 756-Pyramid Transition Matrix
- 59. Spiral Matrix II
- 73.有關棧佇列的演算法實現佇列演算法
- 瞭解Android Matrix轉換Android
- Matrix原始碼分析————Trace Canary原始碼
- Matrix原始碼分析————Resource Canary原始碼
- CF1493F Enchanted Matrix
- ARM的BUS Matrix的作用
- 【矩陣乘法】Matrix Power Series矩陣
- 11.2.0.4單例項ASM安裝報錯ohasd failed to ... line 73.單例ASMAI
- set /?
- Set
- lombok get/set 與 JavaBean get/setLombokJavaBean
- confusion_matrix函式的使用函式