代码一:遍历matrix,分别用两个集合记录需要变化的行和列,然后在遍历设置,空间复杂度为0(m+ns) 1classSolution {2publicvoidsetZeroes(int[][] matrix) {3introw =matrix.length;4if(row==0)return;5intcol = matrix[0].length;67Set<Integer> rowSet =newHashSet<>();8Set<Integer> colSet =newHash...
26for(inti = 1; i < matrix.length; ++i) { 27for(intj = 1; j < matrix[0].length; ++j) { 28if(matrix[i][j] == 0) { 29matrix[i][0] = 0; 30matrix[0][j] = 0; 31} 32} 33} 34 35//set zeroes except the first row and column 36for(inti = 1; i < matrix.length...
public static void setZeroes(int[][]matrix){ HashSet<Integer>row=new HashSet<>(); HashSet<Integer>col=new HashSet<>(); for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[0].length; j++) { if(matrix[i][j]==0){ row.add(i); col.add(j); } ...
Runtime: 1 ms, faster than 99.98% of Java online submissions for Set Matrix Zeroes. Memory Usage: 45.6 MB, less than 0.96% of Java online submissions for Set Matrix Zeroes. class Solution { public void setZeroes(int[][] matrix) { int m = matrix.length, n = matrix[0].length; int ...
【Java代码,O(1)空间】 publicclassSolution{publicvoidsetZeroes(int[][]matrix){intm=matrix.length;intn=matrix[0].length;inti,j;//先标记第一行和第一列是否有0booleanfirstRow=false,firstCol=false;for(j=0;j<n;j++){if(0==matrix[0][j]){firstRow=true;break;}}for(i=0;i<m;i++){if...
LeetCode 73. Set Matrix Zeroes Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. answer: class Solution { public: void setZeroes(vector<vector<int>>& matrix) { set<int> mSet;
Can you solve this real interview question? Set Matrix Zeroes - Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0's. You must do it in place [https://en.wikipedia.org/wiki/In-place_algorithm]. Example 1: [
void setZeroes2(vector<vector<int> > &matrix) { int row = matrix.size(); if (row < 1) return; int col = matrix[0].size(); bool firstRowhasZero = false; bool firstColhasZero = false; for (int i = 0; i < row && !firstColhasZero; i++) ...
Can you solve this real interview question? Set Matrix Zeroes - Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0's. You must do it in place [https://en.wikipedia.org/wiki/In-place_algorithm]. Example 1: [
* 来源:http://oj.leetcode.com/problems/set-matrix-zeroes/ * 结果:AC * 来源:LeetCode * 总结: ***/ #include <iostream> #include <stdio.h> #include <vector> using namespace std; class Solution { public: //时间复杂度 O(m*n),空间复杂度 O(...