Easy C++ CodE with NotE, 0(1) spacE, SimplE....


  • 0
    D

    Simple C++ Solution with Note, O(1) Space.

    CodE ==>>

    class Solution {
        const int setIt = (1 << 27)-1009; /* A Random Number :: Tracker for Defult ZERO(0) */
        const int pik = (1 << 28)-9001; /* A Random Number :: Tracker for Modified ZERO(0) */
    public:
        void setZeroes(vector<vector<int>>& matrix) 
        {
            if(!matrix.size()) return; /* Empty matrix = End_Process */
            int row = matrix.size(), col = matrix[0].size(); /* Numbers of ROW && COLUM for MATRIX */
            
            for(int i = 0; i < row; i++)
                for(int j = 0; j < col; j++)
                    if(!matrix[i][j] || matrix[i][j] == pik)
                    {
                        /* matrix[i][j] = "0 || pik" means, it's a position with value ZERO(0), given by leetcode.com/.... */
                        /* matrix[i][j] = "setIt" means, it's a position with value ZERO(0), modified by me.CODE :)))  */
                        for(int K = 0; K < col; K++) matrix[i][K] = matrix[i][K] == 0 || matrix[i][K] == pik ? pik : setIt;
                        for(int K = 0; K < row; K++) matrix[K][j] = matrix[K][j] == 0 || matrix[K][j] == pik ? pik : setIt;
                    }
            
            for(int i = 0; i < row; i++)
                for(int j = 0; j < col; j++) 
                {
                    /* matrix[i][j] = "setIt || pik" to =====>> ZERO(0) */
                    matrix[i][j] = matrix[i][j] == setIt || matrix[i][j] == pik ? 0 : matrix[i][j];
                }
            /* END_PROCESS */
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.