当前位置: 面试刷题>> 判断数独是否合法 (经典算法题500道)


### 题目描述补充 题目:**判断数独是否合法** 给定一个9x9的二维数组表示的数独棋盘,数组中的每个元素是一个字符('1'-'9'表示数字,'.'表示空位)。请编写一个函数来判断这个数独是否合法。数独合法的条件是: 1. 每行必须恰好包含数字1-9各一次。 2. 每列必须恰好包含数字1-9各一次。 3. 9个3x3的子方格(也称“宫”)必须各自恰好包含数字1-9各一次。 ### 示例代码 #### PHP 示例 ```php function isValidSudoku($board) { $rows = array_fill(0, 9, array_fill(0, 9, false)); $cols = array_fill(0, 9, array_fill(0, 9, false)); $boxes = array_fill(0, 9, array_fill(0, 9, false)); for ($i = 0; $i < 9; $i++) { for ($j = 0; $j < 9; $j++) { $num = $board[$i][$j]; if ($num == '.') continue; $num = intval($num); $boxIndex = floor($i / 3) * 3 + floor($j / 3); if ($rows[$i][$num - 1] || $cols[$j][$num - 1] || $boxes[$boxIndex][$num - 1]) { return false; } $rows[$i][$num - 1] = true; $cols[$j][$num - 1] = true; $boxes[$boxIndex][$num - 1] = true; } } return true; } // 示例用法 $board = [ ["5","3",".",".","7",".",".",".","."], ["6",".",".","1","9","5",".",".","."], [".","9","8",".",".",".",".","6","."], ["8",".",".",".","6",".",".",".","3"], ["4",".",".","8",".","3",".",".","1"], ["7",".",".",".","2",".",".",".","6"], [".","6",".",".",".",".","2","8","."], [".",".",".","4","1","9",".",".","5"], [".",".",".",".","8",".",".","7","9"] ]; echo isValidSudoku($board) ? "数独合法" : "数独不合法"; ``` #### Python 示例 ```python def isValidSudoku(board): rows = [set() for _ in range(9)] cols = [set() for _ in range(9)] boxes = [set() for _ in range(9)] for i in range(9): for j in range(9): num = board[i][j] if num == '.': continue num = int(num) box_index = (i // 3) * 3 + (j // 3) if num in rows[i] or num in cols[j] or num in boxes[box_index]: return False rows[i].add(num) cols[j].add(num) boxes[box_index].add(num) return True # 示例用法 board = [ ["5","3",".",".","7",".",".",".","."], ["6",".",".","1","9","5",".",".","."], [".","9","8",".",".",".",".","6","."], ["8",".",".",".","6",".",".",".","3"], ["4",".",".","8",".","3",".",".","1"], ["7",".",".",".","2",".",".",".","6"], [".","6",".",".",".",".","2","8","."], [".",".",".","4","1","9",".",".","5"], [".",".",".",".","8",".",".","7","9"] ] print("数独合法" if isValidSudoku(board) else "数独不合法") ``` #### JavaScript 示例 ```javascript function isValidSudoku(board) { const rows = Array.from({ length: 9 }, () => new Set()); const cols = Array.from({ length: 9 }, () => new Set()); const boxes = Array.from({ length: 9 }, () => new Set()); for (let i = 0; i < 9; i++) { for (let j = 0; j < 9; j++) { const num = board[i][j]; if (num === '.') continue; const numInt = parseInt(num, 10); const boxIndex = Math.floor(i / 3) * 3 + Math.floor(j / 3); if (rows[i].has(numInt) || cols[j].has(numInt) || boxes[boxIndex].has(numInt)) { return false; } rows[i].add(numInt); cols[j].add(numInt); boxes[boxIndex].add(numInt); } } return true; } // 示例用法 const board = [ ["5","3",".",".","7",".",".",".","."], ["6",".",".","1","9","5",".",".","."], [".","9","8",".",".",".",".","6","."], ["8",".",".",".","6",".",".",".","3"], ["4",".",".","8",".","3",".",".","1"], ["7",".",".",".","2",".",".",".","6"], [".","6",".",".",".",".","2","8","."], [".",".",".","4","1","9",".",".","5"], [".",".",".",".","8",".",".","7","9"] ]; console.log(isValidSudoku(board) ? "数独合法" : "数独不合法"); ``` **码小课网站中有更多相关内容分享给大家学习**,涵盖了算法、数据结构、编程语言等多个领域的深入解析和实践案例。
推荐面试题