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: [
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: [
第i行如果含0,就将第一列matrix[i][0]设为0。这样最后其余所有(n-1)行(m-1)列是否含0的信息都记录在对应第一行,第一列元素里。而第一行,第一列初始是否含0则记录在之前提到的两个布尔值了。 代码 if(matrix.length==0)return;intn=matrix.length;intm=matrix[0].length;booleanfirstRow=false;bool...
附上代码: 1voidsetZeroes(vector<vector<int>>&matrix) {2intn = matrix.size(), m = matrix[0].size();3int*row = (int*)malloc((n /31+2) *sizeof(int));4int*col = (int*)malloc((m /31+2) *sizeof(int));57for(inti =0; i < n /31+2; i ++) row[i] =0;8for(inti ...
if (matrix[i][0] == 0) { left = true; } } for (int i = 0; i < matrix[0].length; i++) {// 上边界是否有零 if (matrix[0][i] == 0) { up = true; } } for (int i = 1; i < matrix.length; i++) { for (int j = 1; j < matrix[0].length; j++) { ...
void setZeroes(vector<vector<int> > &matrix) { int row = matrix.size(); if (row < 1) return; int col = matrix[0].size(); vector<bool> colRecorder(col); vector<bool> rowRecorder(row); for (int i = 0; i < row; i++) ...
public void setZeroes(int[][] matrix) { int m = matrix.length, n = matrix[0].length; int row0 = 1, column0 = 1; // first row for (int i = 0; i < n; i++) { if (0 == matrix[0][i]) { row0 = 0; } }
* 来源: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(...
找到第一个0的位置,将这个位置的行和列作为标记数组。 3. 代码 耗时:53ms class Solution { public: // 遍历找到第一个0,用这个0所在的行和列作为对应行列是否有0的标记位组。 void setZeroes(vector<vector<int>>& matrix) { if (matrix.size() == 0) { return ; } ...
void setZeroes(vector<vector<int> &matrix) { size_t rows = matrix.size(); size_t columns = matrix[0].size(); if (!rows) return; bool isRowZero = false; bool isColumnZero = false; //判断第一行是否有0 for (size_t i = 0; i < columns; ++i) ...