389 - 判断数独是否合法
2017-05-05 16:27
323 查看
5.5
很神奇的使用了map数组,感觉自己的路子简直是有点儿野。
map数组的定义方式为:
HashMap[][] map2 = new HashMap[2][9];
for(int i = 0;i<2;i++){
for(int j = 0;j<9;j++){
map2[i][j] = new HashMap<Character,Integer>();
}
}
判断数独是不是合法, 要判断同一行,同一列,以及同一个九宫之内是不是有相同的数。
虽然AC了,但是提示我的编译是有错误的。
我也不知道错在了哪里,并不想管。
class Solution {
/**
* @param board: the board
@return: wether the Sudoku is valid
*/
public boolean isValidSudoku(char[][] board) {
int m = board.length;
if(m != 9){
return false;
}
int n = board[0].length;
if( n != 9){
return false;
}
HashMap[][] map2 = new HashMap[2][9];
for(int i = 0;i<2;i++){
for(int j = 0;j<9;j++){
map2[i][j] = new HashMap<Character,Integer>();
}
}
// 分成了横三竖三一共九个
for(int i = 0;i < 9;i = i+3){
for(int j = 0;j<9;j= j+3){
HashMap<Character,Integer> map = new HashMap<Character,Integer>();
for(int x = i;x<i+3;x++){
for(int y = j;y<j+3;y++){
if(board[x][y] != '.'){
if(map.containsKey(board[x][y])){
//System.out.println("错误九宫,第" + x +"行,第"+ y+"列");
return false;
}
else{
map.put(board[x][y],1);
}
if(map2[0][x].containsKey(board[x][y])){
// System.out.println("错误行,第" + x +"行,第"+ y+"列");
return false;
}
else{
map2[0][x].put(board[x][y],1);
}
if(map2[1][y].containsKey(board[x][y])){
// System.out.println("错误列,第" + x +"行,第"+ y+"列");
return false;
}
else{
map2[1][y].put(board[x][y],1);
}
}
}
}
}
}
return true;
}
};
很神奇的使用了map数组,感觉自己的路子简直是有点儿野。
map数组的定义方式为:
HashMap[][] map2 = new HashMap[2][9];
for(int i = 0;i<2;i++){
for(int j = 0;j<9;j++){
map2[i][j] = new HashMap<Character,Integer>();
}
}
判断数独是不是合法, 要判断同一行,同一列,以及同一个九宫之内是不是有相同的数。
虽然AC了,但是提示我的编译是有错误的。
我也不知道错在了哪里,并不想管。
class Solution {
/**
* @param board: the board
@return: wether the Sudoku is valid
*/
public boolean isValidSudoku(char[][] board) {
int m = board.length;
if(m != 9){
return false;
}
int n = board[0].length;
if( n != 9){
return false;
}
HashMap[][] map2 = new HashMap[2][9];
for(int i = 0;i<2;i++){
for(int j = 0;j<9;j++){
map2[i][j] = new HashMap<Character,Integer>();
}
}
// 分成了横三竖三一共九个
for(int i = 0;i < 9;i = i+3){
for(int j = 0;j<9;j= j+3){
HashMap<Character,Integer> map = new HashMap<Character,Integer>();
for(int x = i;x<i+3;x++){
for(int y = j;y<j+3;y++){
if(board[x][y] != '.'){
if(map.containsKey(board[x][y])){
//System.out.println("错误九宫,第" + x +"行,第"+ y+"列");
return false;
}
else{
map.put(board[x][y],1);
}
if(map2[0][x].containsKey(board[x][y])){
// System.out.println("错误行,第" + x +"行,第"+ y+"列");
return false;
}
else{
map2[0][x].put(board[x][y],1);
}
if(map2[1][y].containsKey(board[x][y])){
// System.out.println("错误列,第" + x +"行,第"+ y+"列");
return false;
}
else{
map2[1][y].put(board[x][y],1);
}
}
}
}
}
}
return true;
}
};
相关文章推荐
- python_lintcode_96. 链表划分_389. 判断数独是否合法
- lintcode-389-判断数独是否合法
- Python如何判断数独是否合法
- LintCode398:判断数独是否合法
- ACdream 1195 判断数独棋盘是否合法
- 题目:判断数独是否合法
- LintCode Valid Sudoku 判断数独是否合法
- lintcode 容易题:Valid Sudoku 判断数独是否合法
- LintCode-判断数独是否合法
- LintCode算法题解——判断数独是否合法、平面列表、克隆二叉树、序列排号
- 判断数独是否合法
- leetcode 判断数独是否合法
- 36.判断数独是否合法
- LintCode389.判断数独是否合法
- Python如何判断数独是否合法
- 判断数独是否合法-LintCode
- 容易 判断数独是否合法
- ALV判断修改后是否有不合法数据,有则选中错误行,高亮度显示。
- “Network”游戏棋人机对弈的设计与实现(二)—判断步骤是否合法
- 栈-判断某个出栈顺序是否合法