演算法Set Matrix Zeroes
點選(此處)摺疊或開啟
-
public class Solution {
-
public void setZeroes(int[][] matrix) {
-
boolean firstRowZero = false;
-
boolean firstColumnZero = false;
-
-
//set first row and column zero or not
-
for(int i=0; i<matrix.length; i++){
-
if(matrix[i][0] == 0){
-
firstColumnZero = true;
-
break;
-
}
-
}
-
-
for(int i=0; i<matrix[0].length; i++){
-
if(matrix[0][i] == 0){
-
firstRowZero = true;
-
break;
-
}
-
}
-
-
//mark zeros on first row and column
-
for(int i=1; i<matrix.length; i++){
-
for(int j=1; j<matrix[0].length; j++){
-
if(matrix[i][j] == 0){
-
matrix[i][0] = 0;
-
matrix[0][j] = 0;
-
}
-
}
-
}
-
-
//use mark to set elements
-
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;
-
}
-
}
-
}
-
-
//set first column and row
-
if(firstColumnZero){
-
for(int i=0; i<matrix.length; i++)
-
matrix[i][0] = 0;
-
}
-
-
if(firstRowZero){
-
for(int i=0; i<matrix[0].length; i++)
-
matrix[0][i] = 0;
-
}
-
-
}
- }
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29012686/viewspace-1153244/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Leetcode Set Matrix ZeroesLeetCode
- LeetCode 73 Set Matrix ZeroesLeetCode
- Leetcode-Set Matrix ZeroesLeetCode
- Set Matrix Zeroes leetcode javaLeetCodeJava
- Leetcode 73. Set Matrix ZeroesLeetCode
- [CareerCup] 1.7 Set Matrix Zeroes 矩陣賦零矩陣
- leetcode學習筆記73 Set Matrix ZeroesLeetCode筆記
- ZOJ Problem Set - 1094 Matrix Chain MultiplicationAI
- LeetCode—283—Move ZeroesLeetCode
- POJ 3318 Matrix Multiplication(隨機化演算法)隨機演算法
- 演算法練習--LeetCode--54. Spiral Matrix 100%演算法LeetCode
- Matrix Computations
- Matrix Distance
- 283. Move Zeroes--LeetCode RecordLeetCode
- Lintcode539 Move Zeroes solution 題解
- 初探Matrix Android ApkCheckerAndroidAPK
- webgl世界 matrix入門Web
- Leetcode Spiral MatrixLeetCode
- OpenGL Matrix Class (C++)C++
- D. Matrix Cascade
- Factorial Trailing Zeroes 階乘後的零AI
- Leetcode 172. Factorial Trailing ZeroesLeetCodeAI
- leetcode刷題--Factorial Trailing ZeroesLeetCodeAI
- 演算法學習之路|用C++刷演算法會用到的STL(二)——set演算法C++
- Matrix原始碼分析————Trace Canary原始碼
- Matrix原始碼分析————Resource Canary原始碼
- Leetcode 54 Spiral MatrixLeetCode
- 【矩陣乘法】Matrix Power Series矩陣
- 756-Pyramid Transition Matrix
- Leetcode Spiral Matrix IILeetCode
- Leetcode-Spiral MatrixLeetCode
- Spiral Matrix leetcode javaLeetCodeJava
- 每週一練 之 資料結構與演算法(Set)資料結構演算法
- Set
- set /?
- Cellular Matrix 蜂窩矩陣(一)矩陣
- LeetCode 542. 01 MatrixLeetCode
- [LeetCode] 867. Transpose MatrixLeetCode