My c++ solution considering space


  • 0
    G

    Trying to save the space without using too much time.
    I think that checking rows, cols, blocks steps by steps would be relatively efficient?

    bool isValidSudoku(vector<vector<char>>& board) {
      bool checker[9];
      int i,j,k,now;
      int i2,j2;
      //for rows
      for( i = 0 ; i < 9 ; i ++){
         for( j = 0; j < 9 ; j ++ ) checker[j] = false;
         
         for( j = 0; j < 9 ; j ++ ){
             if( board[i][j] == '.' ) continue;
             now =  board[i][j] - 48;
             if( checker[ now - 1 ] ) return false;
             checker[ now - 1 ] = true;
             
         }
         
      }
      //for cols
      for( i = 0 ; i < 9 ; i ++){
         for( j = 0; j < 9 ; j ++ ) checker[j] = false;
         
         for( j = 0; j < 9 ; j ++ ){
             if( board[j][i] == '.' ) continue;
             now =  board[j][i] - 48;
              if( checker[ now - 1 ] ) return false;
             checker[ now - 1 ] = true;
            
         }
         
      }
      //for blocks
      for( i = 0 ; i < 9 ; i ++){
         for( j = 0; j < 9 ; j ++ ) checker[j] = false;
         
         for( j = 0; j < 9 ; j ++ ){
             i2 = (i/3)*3 + (j/3);
             j2 = (i%3)*3 + (j%3);
             if( board[i2][j2] == '.' ) continue;
             now =  board[i2][j2] - 48;
             if( checker[ now - 1 ] ) return false;
             checker[ now - 1 ] = true;
             
         }
         
      }     
      
      return true;
    }

  • 0
    H

    You can try to factor out the checker initializations.


Log in to reply
 

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