Valid Sudoku-数独判断
2016-05-03 16:00
253 查看
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 boolean isValidSudoku(char[][] board) {
for(int i = 0; i < 9; i++){
HashSet<Character> Row = new HashSet<Character>();
HashSet<Character> Col = new HashSet<Character>();
HashSet<Character> Cube = new HashSet<Character>();
for(int j = 0; j < 9; j++){
if(board[i][j]!='.'&&!Row.add(board[i][j])){
return false;
}
if(board[j][i]!='.'&&!Col.add(board[j][i])){
return false;
}
int RowIndex = 3*(i/3);
int ColIndex = 3*(i%3);
if(board[RowIndex + j/3][ColIndex + j%3]!='.' && !Cube.add(board[RowIndex + j/3][ColIndex + j%3]))
return false;
}
}
return true;
}
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 boolean isValidSudoku(char[][] board) {
for(int i = 0; i < 9; i++){
HashSet<Character> Row = new HashSet<Character>();
HashSet<Character> Col = new HashSet<Character>();
HashSet<Character> Cube = new HashSet<Character>();
for(int j = 0; j < 9; j++){
if(board[i][j]!='.'&&!Row.add(board[i][j])){
return false;
}
if(board[j][i]!='.'&&!Col.add(board[j][i])){
return false;
}
int RowIndex = 3*(i/3);
int ColIndex = 3*(i%3);
if(board[RowIndex + j/3][ColIndex + j%3]!='.' && !Cube.add(board[RowIndex + j/3][ColIndex + j%3]))
return false;
}
}
return true;
}
相关文章推荐
- HBase中的HMaster、HRegionServer、Zookeeper
- Intellij IDEA 添加项目依赖
- HDFS详解
- js 按钮下一个上一个滚动效果
- 小知识,用myeclipes找jar
- 持续集成套件安装
- vector的简易实现
- Retrofit和RxJava结合使用例子分析
- 第9条 对于所有对象都通用的方法——覆盖equals时总要覆盖HashCode
- 详解JavaScript异步编程中jQuery的promise对象的作用
- 系统函数dlopen()被劫持导致symbol找不到的问题记录
- activemq 基本使用案例
- iOS开发 剖析网易新闻标签栏视图切换(addChildViewController属性介绍)
- 记《learning hard C#学习笔记》 书中一个错误
- 234. Palindrome Linked List
- Android Studio 改变 SDK 的源码路径
- Sping 自己学习心得(IOC)
- JavaFX
- java基础-Map
- 使用新的 Android Studio 加速你的开发