2016年2月15日 星期一

LEET code -- Valid Sudoku

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.
The Sudoku board could be partially filled, where empty cells are filled with the character '.'.


A partially filled sudoku which is valid.

Note:
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.


Subscribe to see which companies asked this question


檢查是不是合法的數獨(不用一定需要解答  合法就好

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
bool isValidSudoku(char** board, int boardRowSize, int boardColSize) {
    for(int i=0;i<boardRowSize;i++){
        for(int j=0;j<boardColSize;j++){
            if(board[i][j]!='.'){
                int counter=0,target=board[i][j];
                for(int r=0;r<boardColSize;r++)if(board[r][j]==target)counter++;
                if(counter!=1)return false;
                for(int c=0;c<boardRowSize;c++)if(board[i][c]==target)counter++;
                if(counter!=2)return false;
                for(int r=(i/3)*3;r<(i/3)*3+3;r++){
                    for(int c=(j/3)*3;c<(j/3)*3+3;c++){
                        if(board[r][c]==target)counter++;
                    }
                }
                if(counter!=3)return false;
            }
        }
    }
    return true;
}

沒有留言:

張貼留言