[RainbowSecret] C++ implementation


  • 0

    The error I made during the solving is that I forget to change the number from [1..9] to [0..8] use

        int temp=board[i][j]-'1'+1;
    

    But should use

      int temp=board[i][j]-'1';
    

    As we use the array dimention is 9 , so the index is [0..8]

    Here is the AC code:

    class Solution {
    public:
        bool isValidSudoku(vector<vector<char>>& board) {
            bool c1[9][9]={false}, c2[9][9]={false}, c3[9][9]={false};
            for(int i=0; i<9; i++){
                for(int j=0; j<9; j++){
                    if(board[i][j]!='.'){
                        int temp=board[i][j]-'1';
                        int index=(i/3)*3+j/3;
                        if(c1[i][temp] || c2[j][temp] || c3[index][temp])  return false;
                        c1[i][temp]=c2[j][temp]=c3[index][temp]=true;
                    }
                }
            }
            return true;
        }
    };

Log in to reply
 

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