[leet code] Valid Sudoku
来源:互联网 发布:js时间滑动选择插件 编辑:程序博客网 时间:2024/06/07 00:00
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.
This problem is strait forward, we need only implementing all the 3 rules of Sudoku.The only thing we need to concern about is how to determine if numbers (in each rule) are duplicated. Accordingly, there are 2 approaches:
1. use HashSet.add to determine (if duplicate value added, method would return false)
2. use boolean array[265] (representing all the ASCII characters) to determine. Set boolean value true when new number (character) read. If the value has already true, then a duplicate value read.
Approach 1:
public class Solution { public boolean isValidSudoku(char[][] board) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. // rule1, column for(int i=0; i<board[0].length; i++){ HashSet<Character> test = new HashSet<Character>(); for(int j=0; j<board.length; j++){ if(board[j][i]!='.' && !test.add(board[j][i])) return false; } } // rule2, row for(int i=0; i<board.length; i++){ HashSet<Character> test = new HashSet<Character>(); for(int j=0; j<board[0].length; j++){ if(board[i][j]!='.' && !test.add(board[i][j])) return false; } } // rule3, sub-box for(int i=0; i<3; i++){ for(int j=0; j<3; j++){// for each sub-box HashSet<Character> test = new HashSet<Character>(); for(int m=i*3; m<i*3+3; m++){//row for(int n=j*3; n<j*3+3; n++){//column if(board[m][n]!='.' && !test.add(board[m][n])) return false; } } } } return true; }}
Approach2:
public class Solution { public boolean isValidSudoku(char[][] board) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. // rule1, column for(int i=0; i<board[0].length; i++){ boolean[] dupCheck = new boolean[256]; for(int j=0; j<board.length; j++){ if(board[j][i]!='.'){ if(dupCheck[board[j][i]]==true) return false; else dupCheck[board[j][i]] = true; } } } // rule2, row for(int i=0; i<board.length; i++){ boolean[] dupCheck = new boolean[256]; for(int j=0; j<board[0].length; j++){ if(board[i][j]!='.'){ if(dupCheck[board[i][j]]==true) return false; else dupCheck[board[i][j]] = true; } } } // rule3, sub-box for(int i=0; i<3; i++){ for(int j=0; j<3; j++){// for each sub-box boolean[] dupCheck = new boolean[256]; for(int m=i*3; m<i*3+3; m++){//row for(int n=j*3; n<j*3+3; n++){//column if(board[m][n] != '.'){ if(dupCheck[board[m][n]]==true) return false; else dupCheck[board[m][n]] = true; } } } } } return true; }}
0 0
- [leet code] Valid Sudoku
- [leet code] Valid Parentheses
- CODE 97: Valid Sudoku
- Leet Code 20 Valid Parentheses
- Leet Code OJ: Valid Number
- 【Leet Code】 242. Valid Anagram--easy
- leetcode 日经贴,Cpp code -Valid Sudoku
- Leet Code OJ 242. Valid Anagram [Difficulty: Easy]
- Leet Code OJ 20. Valid Parentheses [Difficulty: Easy]
- Leet Code OJ 125. Valid Palindrome [Difficulty: Easy]
- Leet Code OJ 20. Valid Parentheses [Difficulty: Easy]
- Valid Sudoku
- Valid Sudoku
- Valid Sudoku
- Valid Sudoku
- Valid Sudoku
- Valid Sudoku
- Valid Sudoku
- [深夜水一水]POJ 3627Bookshelf
- 自己搭建远程服务器成功,可以远程访问哦
- linux下的网络程序0——服务器端返回客户端输入
- petsc 结构化数据使用
- 【C#小知识】C#中一些易混淆概念总结(二)--------构造函数,this关键字,部分类,枚举
- [leet code] Valid Sudoku
- 蓝桥杯练习题-《道路和航路》解题报告
- [完全背包]HDOJ 1248寒冰王座
- one-to-one外键双向关联之建表
- hdu 2243 ac自动机+矩阵快速幂
- [VB.NET] 多語言文字加密器
- 【C#小知识】C#中一些易混淆概念总结(二)
- Sum It Up
- 设计模式-工厂模式(二)