leetcode #73 in cpp

原创 2016年05月31日 22:34:28

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.

Solution 1: O(m+n) space.

We can use two arrays to record which row and which column should be set zero. Once we know all of them, we can start to set 0. 

Code:

class Solution {
public:
    void setZeroes(vector<vector<int>>& matrix) {
        if(matrix.empty()) return;
        
        vector<int> row_zero;
        vector<int> col_zero;
        
        for(int i = 0; i < matrix.size(); i ++){
            for(int j = 0; j < matrix[0].size(); j ++){
                if(matrix[i][j] == 0){//record which row and which column should be set 0 
                    row_zero.push_back(i);
                    col_zero.push_back(j);
                }
            }
        }
        for(int i = 0; i < row_zero.size(); i ++){//set row to 0 
            for(int j = 0; j < matrix[0].size(); j ++)
                matrix[row_zero[i]][j] = 0;
        }
        for(int j = 0; j < col_zero.size(); j++){//set column = 0
            for(int i = 0; i < matrix.size(); i ++){
                matrix[i][col_zero[j]] = 0;
            }
        }
    }
};


Solution 2: recursion, O(m+n) space

In each recursion, we find next 0 that is not found in previous recursion. Once we find it, we go to next recursion to search other 0s. When next recursion ends, we set the corresponding row and column to zeros and go back to previous recursion. In this way the location is retained while we could perform next search. 

Code: 

class Solution {
public:
    void setZeroes(vector<vector<int>>& matrix) {
        setZeros2(matrix,0,0);
    }
    void setZeros2(vector<vector<int>>& matrix,int row, int col){//search by row

        while(row < matrix.size()){//start looking for 0 from position (row,col)
            while(col<matrix[0].size()){
                if(matrix[row][col] == 0) {
                    break;
                }
                col++;
            }
            if(col!=matrix[0].size()) break;//means we have found 1 0
            row ++;
            col = 0;
        }
        if(row>=matrix.size()) return;//if we dont find 0 any more, end this recurrence and go back to previous recurrence.
        
        //set next starting point for searching next 0
        setZeros2(matrix, col+1 == matrix[0].size()?row+1:row, (col+1)%matrix[0].size());//go to next search
        
        //when next search ends its setting 0, set 0 for current recurrence
        for(int i = 0; i < matrix.size(); i ++){
            matrix[i][col] = 0;
        }
        
        for(int i = 0; i < matrix[0].size(); i ++){
            matrix[row][i] = 0;
        }
        return;
        
    }
};

Solution 3: constant space

When we find a 0 at position(i,j), we know that ith row and jth column should be zero. To record this, we set mat[0][j] = 0 and mat[0][i] = 0. In this way, the first row and the first column serves as a flag. We also have to use 2 flags to record if our first row and first column should be set 0. This is because during finding 0s, the first row and column is changed as they are flags. We need to record if they are originally supposed to be 0. 

class Solution {
public:
    void setZeroes(vector<vector<int>>& matrix) {
        setZeros_constant_space(matrix);
    }
    void setZeros_constant_space(vector<vector<int>>& matrix){//search by row
        if(matrix.empty()) return;
        
        bool firstRowZero = false;
        bool firstColZero = false;
        int m = matrix.size();
        int n = matrix[0].size();
        
        for(int i = 0; i < m; i++){//see if the first column should be set 0
            if(matrix[i][0] == 0) firstColZero = true;
        }
        for(int i = 0; i < n; i++){//see if the first row should be set 0
            if(matrix[0][i] == 0) firstRowZero = true;
        }
        
        int row = 1;
        int col = 1;
        
        while(row<m){
            
            while(col < n){//find 0. once a 0 is found, project it to the first row and first column. set those two positions to 0
                if(matrix[row][col] == 0){
                    matrix[0][col] = 0;
                    matrix[row][0] = 0;
                }
                col++;
            }
            
            col = 1;
            row++;
        }
        
        //set 0 
        for(int i = 1; i < m; i ++){//if mat[i][0] = 0, it means the ith row should be zero.
            if(matrix[i][0] == 0)
                for(int j = 0; j < n; j ++)
                    matrix[i][j] = 0;
        }
        
        for(int i = 1; i < n; i ++){//if mat[0][i] = 0, it means ith column should be zero
            if(matrix[0][i] == 0)
                for(int j = 0; j < m; j ++)
                    matrix[j][i] = 0;
        }
        if(firstRowZero)//if first row should be zero, set it 0
            for(int j = 0; j < n; j ++)
                    matrix[0][j] = 0;
        if(firstColZero)//if first column should be zero, set it 0
            for(int j = 0; j < m; j ++)
                    matrix[j][0] = 0;
            
        
    }
}


LeetCode73 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. Foll...
  • Lu597203933
  • Lu597203933
  • 2015年04月07日 09:19
  • 1197

leetcode-73. Set Matrix Zeroes

leetcode-73. Set Matrix Zeroes题目: 这里的思路是先从左上到右下遍历,将所有需要需要置零的行和列的首位都置为0,然后再反向遍历将所有首位为0的行和列都置为0。这里还用一...
  • u011518120
  • u011518120
  • 2016年12月18日 21:49
  • 252

leetcode 73

O(m + n)空间方法:设置两个bool 数组,记录每行和每列是否存在0。要想常数空间,可以复用第一行第一列。 ’ class Solution { public: void setZero...
  • peter19930127
  • peter19930127
  • 2017年06月22日 15:34
  • 39

[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. ...
  • SunnyYoona
  • SunnyYoona
  • 2014年01月23日 20:45
  • 1892

leetcode_question_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. Foll...
  • doc_sgl
  • doc_sgl
  • 2013年09月19日 21:47
  • 4309

LeetCode --- 73. Set Matrix Zeroes

题目链接:Set Matrix Zeroes Given a m x n matrix, if an element is 0, set its entire row and column to 0...
  • makuiyu
  • makuiyu
  • 2015年03月20日 21:50
  • 1481

LeetCode 73

原题: Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it i...
  • k829593756
  • k829593756
  • 2017年10月09日 13:35
  • 42

LeetCode Solution(持续更新,java>c++)

Title Add Date AC Rates
  • disappearedgod
  • disappearedgod
  • 2014年04月13日 21:52
  • 8830

[leetcode] 【分治法】 50. Pow(x, n)

Implement pow(x, n). 题意 实现幂函数。 题解 使用分治法求解。 分:将n分成n/2  直到n=0时,返回1; 治:对n为偶数,返回两数相乘的结果,奇数再乘多一个x...
  • u014654002
  • u014654002
  • 2016年06月21日 00:37
  • 381

LeetCode 73 — Set Matrix Zeroes(C++ Java Python)

题目:http://oj.leetcode.com/problems/set-matrix-zeroes/ Given a m x n matrix, if an element is 0, set...
  • dragon_dream
  • dragon_dream
  • 2014年04月01日 18:27
  • 2270
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode #73 in cpp
举报原因:
原因补充:

(最多只允许输入30个字)