Valid Sudoku
来源:互联网 发布:淘宝的车秒贷怎么样 编辑:程序博客网 时间:2024/06/02 18:16
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.
思路: 就是检查一遍里面的每个点是否合法.
易错点: 别忘了排除 同一个位置的。
public class Solution { public boolean isValidSudoku(char[][] board) { for(int i = 0; i < 9; i++){ for(int j = 0; j < 9; j++){ if(board[i][j] != '.'){ if(!isValid(board, i, j)) return false; } } } return true; } private boolean isValid(char[][] board, int x, int y){ char c = board[x][y]; for(int i = 0; i < 9; i++){ if(i != y && board[x][i] == c) return false; if(i != x && board[i][y] == c) return false; int blockRow = 3 * (x / 3) + i / 3; int blockCol = 3 * (y / 3) + i % 3; if((blockRow != x || blockCol != y) && board[blockRow][blockCol] == c) return false; } return true; }}
0 0
- Valid Sudoku
- Valid Sudoku
- Valid Sudoku
- Valid Sudoku
- Valid Sudoku
- Valid Sudoku
- Valid Sudoku
- Valid Sudoku
- Valid Sudoku
- Valid sudoku
- Valid Sudoku
- Valid Sudoku
- Valid Sudoku
- Valid Sudoku
- Valid Sudoku
- Valid Sudoku
- Valid Sudoku
- Valid Sudoku
- EIP & EBP & ESP
- HDU 4046 Panda(RMQ 线段树 树状数组)
- Java final关键字
- dom4j 两种创建XML Document方法中命名空间的差异!
- PAT1010. 一元多项式求导 (25)(未通过)
- Valid Sudoku
- MFC中的文件读写方法总结
- Android-SharedPreferences读写-简单
- Oracle spool 用法小结
- 金蝶EAS消息弹出框样式
- Scrum之 站立例会
- [Leetcode 191, Easy] Number of 1 Bits
- PAT1011A+B和C (15)
- 读懂Java中的Socket编程