本文主要是介绍Java | Leetcode Java题解之第73题矩阵置零,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目:
题解:
class Solution {public void setZeroes(int[][] matrix) {int m = matrix.length, n = matrix[0].length;boolean flagCol0 = false;for (int i = 0; i < m; i++) {if (matrix[i][0] == 0) {flagCol0 = true;}for (int j = 1; j < n; j++) {if (matrix[i][j] == 0) {matrix[i][0] = matrix[0][j] = 0;}}}for (int i = m - 1; i >= 0; i--) {for (int j = 1; j < n; j++) {if (matrix[i][0] == 0 || matrix[0][j] == 0) {matrix[i][j] = 0;}}if (flagCol0) {matrix[i][0] = 0;}}}
}
这篇关于Java | Leetcode Java题解之第73题矩阵置零的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!