个人记录-LeetCode 36. Valid Sudoku
2016-11-28 20:12
302 查看
问题:
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 ‘.’.
Note:
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
代码示例:
1、分别检查每一行、每一列、每个独立的单元,判断是否满足有效数独问题的条件。
这个方法是最容易想到的,但需要重复扫描整个数据3次,比较费时。
2、建立映射关系
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 ‘.’.
Note:
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
代码示例:
1、分别检查每一行、每一列、每个独立的单元,判断是否满足有效数独问题的条件。
这个方法是最容易想到的,但需要重复扫描整个数据3次,比较费时。
public class Solution { public boolean isValidSudoku(char[][] board) { return isValid(board) && checkAllTheRow(board) && checkAllTheColumn(board) && checkAllUnits(board); } //检查输入数据是否正常 private boolean isValid(char[][] board) { if (board == null) { return false; } if (board.length != 9) { return false; } for (int i = 0; i < 9; ++i) { if (board[i].length != 9) { return false; } } return true; } //检查每一行是否正常,即1~9中的数字只出现一次 private boolean checkAllTheRow(char[][] board) { TreeSet<Integer> count = new TreeSet<>(); for (int i = 0; i < 9; ++i) { for (int j = 0; j < 9; ++j) { if (board[i][j] != '.') { int temp = board[i][j] - '0'; if (temp < 1 || temp > 9 || count.contains(temp)) { return false; } count.add(temp); } } count.clear(); } return true; } //检查每一列是否正常 private boolean checkAllTheColumn(char[][] board) { TreeSet<Integer> count = new TreeSet<>(); for (int j = 0; j < 9; ++j) { for (int i = 0; i < 9; ++i) { if (board[i][j] != '.') { int temp = board[i][j] - '0'; if (temp < 1 || temp > 9 || count.contains(temp)) { return false; } count.add(temp); } } count.clear(); } return true; } //依次检查每一个独立的单元是否正常 private boolean checkAllUnits(char[][] board) { int rowBegin = 0; int columnBegin = 0; for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { if (!checkUnit(board, rowBegin, columnBegin)) { return false; } columnBegin += 3; } rowBegin += 3; columnBegin = 0; } return true; } //检查一个独立的单元 private boolean checkUnit(char[][] board, int rowBegin, int columnBegin) { TreeSet<Integer> count = new TreeSet<>(); for (int i = rowBegin; i < rowBegin + 3; ++i) { for (int j = columnBegin; j < columnBegin + 3; ++j) { if (board[i][j] != '.') { int temp = board[i][j] - '0'; if (temp < 1 || temp > 9 || count.contains(temp)) { return false; } count.add(temp); } } } return true; } }
2、建立映射关系
public class Solution { public boolean isValidSudoku(char[][] board) { return isValid(board) && check(board); } private boolean isValid(char[][] board) { if (board == null) { return false; } if (board.length != 9) { return false; } for (int i = 0; i < 9; ++i) { if (board[i].length != 9) { return false; } } return true; } private boolean check(char[][] board) { //row存储每一行的信息,column存储每一列的信息,unit存储每个单元的信息 boolean row[][] = new boolean[9][9]; boolean column[][] = new boolean[9][9]; boolean unit[][] = new boolean[9][9]; for (int i = 0; i < 9; ++i) { for(int j = 0; j < 9; ++j) { if (board[i][j] == '.') { continue; } int loc = board[i][j] - '1'; //这里就是每个单元的转换关系稍微复杂一点,稍微做一下计算即可理解 if (row[i][loc] || column[j][loc] || unit[(i/3) * 3 + j/3][loc]) { return false; } row[i][loc] = true; column[j][loc] = true; unit[(i/3) * 3 + (j/3)][loc] = true; } } return true; } }
相关文章推荐
- 个人记录-LeetCode 3.Longest Substring Without Repeating Characters
- 个人记录-LeetCode 5.Longest Palindromic Substring
- 个人记录-LeetCode 21. Merge Two Sorted Lists
- 个人记录-LeetCode 26. Remove Duplicates from Sorted Array
- 个人记录-LeetCode 25. Reverse Nodes in k-Group
- 个人记录-LeetCode 1.Two Sum
- 个人记录-LeetCode 30. Substring with Concatenation of All Words
- 个人记录-LeetCode 19. Remove Nth Node From End of List
- 个人记录-LeetCode 18. 4Sum
- 个人记录-LeetCode 28. Implement strStr()
- 个人记录-LeetCode 6.ZigZag Conversion
- 个人记录-LeetCode 11. Container With Most Water
- 个人记录-LeetCode 10.Regular Expression Matching
- 个人记录-LeetCode 20. Valid Parentheses
- 个人记录-LeetCode 15. 3Sum
- 个人记录-LeetCode 4.Median of Two Sorted Arrays
- 个人记录-LeetCode 9.Palindrome Number
- 个人记录-LeetCode 13. Roman to Integer
- 个人记录-LeetCode 23. Merge k Sorted Lists
- 个人记录-LeetCode 29. Divide Two Integers