标签:
https://leetcode.com/problems/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.
正好用了一下最近get的新姿势,没有用map存1-9是否访问过。
一个变量_bit就记下了这新信息,用位运算。
比如来的数是4,就是100,与_bit做位与,如果没有出现过100&000是0,否则100&100不等于0,最后用100|000记下4已经访问过了。
人参第一次使用四重循环。这种酸爽不敢相信,我用脚趾猜row,column,square这3种情况可以合并,但是为了纪念我人生的第一次四重循环,就这样吧(其实是懒。
1 /** 2 * @param {character[][]} board 3 * @return {boolean} 4 */ 5 var isValidSudoku = function(board) { 6 this._bit = 0; 7 function isValid(num){ 8 var tmp = Math.pow(2, num - 1); 9 if((this._bit & tmp) !== 0){ 10 return false; 11 }else{ 12 this._bit = this._bit | tmp; 13 return true; 14 } 15 } 16 17 var i = 0, j = 0, m = 0, n =0, cell = 0; 18 //row 19 for(i = 0; i < 9; i++){ 20 for(j = 0; j < 9; j++){ 21 cell = parseInt(board[i][j]); 22 if(!isValid(cell)){ 23 return false; 24 } 25 } 26 this._bit = 0; 27 } 28 //column 29 for(i = 0; i < 9; i++){ 30 for(j = 0; j < 9; j++){ 31 cell = parseInt(board[j][i]); 32 if(!isValid(cell)){ 33 return false; 34 } 35 } 36 this._bit = 0; 37 } 38 //square 39 for(i = 0; i <= 6; i+=3){ 40 for(j = 0; j <=6 ; j+=3){ 41 for(m = 0; m < 3; m++){ 42 for(n = 0; n < 3; n++){ 43 cell = parseInt(board[m + i][n + j]); 44 if(!isValid(cell)){ 45 return false; 46 } 47 } 48 } 49 this._bit = 0; 50 } 51 } 52 return true; 53 };
[LeetCode][JavaScript]Valid Sudoku
标签:
原文地址:http://www.cnblogs.com/Liok3187/p/4513428.html