LeetCode 在需要接觸演算碼的工程師中,為相當知名的練習場所。當你有需要挑戰自己的腦筋、面試前的準備……等,都滿適合來這邊看一下你的演算法和資料結構的熟悉程度。這回要筆記的有 Valid Sudoku。
題目
36. Valid Sudoku
[連結]:https://leetcode.com/problems/valid-sudoku/
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51  | 
						/* * 1. 給定三個 Array (row, column, box) * 2. 宣告一個雙重迴圈,並一次遍歷取出一組 row, column 和 box * 3. row 為 [i][j], column 為 [j][i], box 為 [3 * Math.floor(i/3) + Math.floor(j/3)][3 * (i%3) + j%3] * 4. 如果取出的值為 '.' 則跳過,否則就看 [] 裡有沒有重複的值(用 indexOf 判斷) * 5. 若 indexOf === -1,那就推入 array ,否則就回傳 false * 6. 若全部都通過測試,最後回傳 true */ /**  * @param {character[][]} board  * @return {boolean}  */ var isValidSudoku = function(board) {     for(let i=0; i<9;i++){         let row = []         let column = []         let box = []         for(let j=0;j<9;j++){             let rowValue = board[i][j]             let columnValue = board[j][i]             let boxValue = board[3 * Math.floor(i/3) + Math.floor(j/3)][3 * (i%3) + j%3]             if(rowValue !== '.'){                 if(row.indexOf(rowValue) === -1){                     row.push(rowValue)                 }else{                     return false                 }             }             if(columnValue !== '.'){                 if(column.indexOf(columnValue) === -1){                     column.push(columnValue)                 }else{                     return false                 }             }             if(boxValue !== '.'){                 if(box.indexOf(boxValue) === -1){                     box.push(boxValue)                 }else{                     return false                 }                }         }     }     return true    };  | 
					

