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.

Follow up:
Did you use extra space?A straight forward solution using O(m n) space is probably a bad idea.A simple improvement uses O(m + n) space, but still not the best solution.Could you devise a constant space solution?

分析

给定一个数组,如果某个元素为0,将其整行与整列都改为0.题目提示可以通过常数内存空间就可解决。我是用m+n的数组来表示某行或某列是否需要改为0.
有些人用matrix的第一行与第一列保存该行或该列是否存在0,这样就不需要额外的内存空间了。

void setZeroes(int** matrix, int matrixRowSize, int matrixColSize) {
    int rowflag[matrixRowSize],colflag[matrixColSize];
    for(int i=0;i

你可能感兴趣的:(Leetcode 73. Set Matrix Zeroes)