389

来源:互联网 发布:钉钉 心知天气 编辑:程序博客网 时间:2024/06/06 04:39

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;    }};


0 0
原创粉丝点击