https://leetcode.com/problems/valid-sudoku/
Valid Sudoku
Determine if a Sudoku is valid, according To:sudoku puzzles-the Rules.
The Sudoku board could be partially filled, where empty cells is 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.
Just used a new pose for the latest get, and did not use the map to save 1-9 whether or not to visit.
A variable _bit The new information, using bit arithmetic.
For example, the number is 4, is 100, and _bit do bit with, if not appear 100&000 is 0, otherwise 100&100 is not equal to 0, and finally with 100|000 Note 4 has been visited.
Ginseng uses four cycles for the first time. This acid, I can't believe it, I guess row,column,square with my toes. These 3 things can be combined, but in order to commemorate my first four-cycle life, this is it (actually lazy.)
1 /**2 * @param {character[][]} board3 * @return {Boolean}4 */5 varIsvalidsudoku =function(board) {6 This. _bit = 0;7 functionisValid (num) {8 varTMP = Math.pow (2, Num-1);9 if(( This. _bit & tmp)!== 0){Ten return false; One}Else{ A This. _bit = This. _bit |tmp; - return true; - } the } - - vari = 0, j = 0, m = 0, n =0, cell = 0; - //Row + for(i = 0; i < 9; i++){ - for(j = 0; J < 9; J + +){ +Cell =parseint (Board[i][j]); A if(!isValid (cell)) { at return false; - } - } - This. _bit = 0; - } - //column in for(i = 0; i < 9; i++){ - for(j = 0; J < 9; J + +){ toCell =parseint (Board[j][i]); + if(!isValid (cell)) { - return false; the } * } $ This. _bit = 0;Panax Notoginseng } - //Square the for(i = 0; I <= 6; i+=3){ + for(j = 0; J <=6; j+=3){ A for(m = 0; m < 3; m++){ the for(n = 0; n < 3; n++){ +Cell = parseint (board[m + i][n +j]); - if(!isValid (cell)) { $ return false; $ } - } - } the This. _bit = 0; - }Wuyi } the return true; -};
[Leetcode] [JavaScript] Valid Sudoku