Lintcode389 Valid Sudoku solution 题解

【Title description】

 Determine whether a Sudoku is valid.

The Sudoku board could be partially filled, where empty cells are filled with the character..

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

Please determine whether a Sudoku is valid.

The Sudoku may only be partially filled with numbers, and the missing numbers are indicated by ..

[Note] A valid Sudoku (only partially filled) is not necessarily solvable. We just need to make the padded spaces valid.

 

【Title link】

www.lintcode.com/en/problem/valid-sudoku/

 

【Topic Analysis】

This question uses the uniqueness of HashSet to help check.

Press each line to check first. If it is '.', it means that the word has not been filled in. It is legal. Go down. If it is not stored in the set, add it. If the key value that exists in the set appears during the convenience process, Indicates that there are duplicate numbers in one line, which is invalid and returns false.

Then follow this method to check the column. Finally, check the small square according to this method.

Pay attention to the ij method of the small square. For the current board, there are a total of 9 small squares, numbered from left to right from 0 to 8.

To find '/' by number is to find the abscissa of the first row of the current small square, because each small square has 3 rows, so loop 3 times.

To find '%' by number is to find the ordinate of the first column of the current small square, because each small square has 3 columns, so loop 3 times.

The work of checking the small squares is completed by walking aside the 9 small squares in turn.

 

【Reference answer】

www.jiuzhang.com/solutions/valid-sudoku/

 

Copyright belongs to the author. For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324690966&siteId=291194637