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: [
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. click to show follow up. Follow up: Did you use extra space? A straight forward solution using O(mn) space is probably a bad idea. A simple improvement uses O(m + n) space, but...
附上代码: 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 ...
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: [
【LeetCode】Set Matrix Zeroes 解题报告 【题目】 Given amxnmatrix, if an element is 0, set its entire row and column to 0. Do it in place. click to show follow up. Follow up: Did you use extra space? A straight forward solution using O(mn) space is probably a bad idea....
class Solution { 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; ...
【摘要】 Leetcode 题目解析之 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. Follow up: Did you use extra space? A straight forward solution using O(m__n) space is probably a bad idea. ...
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;
Could you devise a constant space solution? 利用额外空间O(m+n)的做法是很简洁的: void setZeroes(vector<vector<int> > &matrix) { int row = matrix.size(); if (row < 1) return; int col = matrix[0].size(); vector<bool> colRecorder(col); ...
顺便说一下leetcode 解法一说的解法,思想是一样的,只不过它没有用 bool 数组去标记,而是用两个 set 去存行和列。 classSolution{publicvoidsetZeroes(int[][]matrix){intR=matrix.length;intC=matrix[0].length;Set<Integer>rows=newHashSet<Integer>();Set<Integer>cols=newHashSet<Integer>();// 将元素...