LeetCode2.1.14 @ Valid Sudoku 验证数独D2F2

来源:互联网 发布:淘宝数据包 编辑:程序博客网 时间:2024/06/02 06:21

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.

There are just 3 rules to Sudoku.

Each row must have the numbers 1-9 occuring just once.
Each column must have the numbers 1-9 occuring just once.
And the numbers 1-9 must occur just once in each of the 9 sub-boxes of the grid.

用HashMap记录2D-Array的值,再查重

public class Solution {    public boolean isValidSudoku(char[][] board) {                for (int i = 0; i < 9; i++) {                  HashMap<Character, Character> hm = new HashMap<Character, Character>();                  for (int j = 0; j < 9; j++) {                      char c = board[i][j];//【注意1】second[] change more freq,so it's checking rowSet                      if (c=='.') {                                                }    else{  //【注意2】                        if (hm.get(c)==null) {                              hm.put(c, c);                          }else {                              return false;                          }                      }                  }              }                        for (int i = 0; i < 9; ++i) {                  HashMap<Character, Character> hm = new HashMap<Character, Character>();                  for (int j = 0; j < 9; ++j) {                      char c = board[j][i];//【注意1】first[] change more freq,so it's checking colSet                       if (c=='.') {                                            }    else{ //【注意2】                         if (hm.get(c)==null) {                              hm.put(c, c);                          }else {                              return false;                          }                      }                  }              }                return isSmallValid(board, 0, 0) && isSmallValid(board, 0, 3)  && isSmallValid(board, 0, 6)                 && isSmallValid(board, 3, 0) && isSmallValid(board, 3, 3)  && isSmallValid(board, 3, 6)                  && isSmallValid(board, 6, 0) && isSmallValid(board, 6, 3)  && isSmallValid(board, 6, 6);          }            private boolean isSmallValid(char[][] board, int x, int y) { //【注意3】x,y:start-row-index,start-col-index of the subbox,they are included.            HashMap<Character, Character> hm = new HashMap<Character, Character>();              for (int i = x; i < 3 + x; i++) {                  for (int j = y; j < 3 + y; j++) {                      char c = board[i][j];                      if (c=='.') {                                            }    else{                         if (hm.get(c)==null) {                              hm.put(c, c);                          }else {                              return false;                          }                      }                  }              }              return true;          }  }

【注意1】无论check行集合,还是列集合,都是外圈i,里圈j(别用row和col做变量,否则易错)。
关键在于board[i][j],还是board[j][i]。
【注意2】if--else的逻辑 VS if--else if--else 完全不一样!在这里出错了。
【注意3】isSmallValid的参数是char[][]。 因为写成char[]编译错误。
【注意4】每个for,if,else请配上括号!因为这个问题使结果错误。


附:第一次写的错误code

public class Solution {    public boolean isValidSudoku(char[][] board) {        //check for subbox        HashSet<Character> subboxSet=new HashSet<Character>();        for(int i=0;i<3*3;i+=3)            for(int j=0;j<3*3;j+=3)                for(int r=0;r<3;r++)                    for(int c=0;c<3;c++){                        if(board[i+r][j+c]=='.'){  //no matter what the loop invariant, 2D-Array is always [row][col]                            //do nothing                            }                        else if(subboxSet.contains(board[i+r][j+c]))                            return false;                        else                            subboxSet.add(board[i+r][j+c]);                    }//end inner for                            //check for ColmnSet,out loop is col,skill        HashSet<Character> colSet=new HashSet<Character>();        for(int c=0;c<9;c++)            for(int r=0;r<9;r++){                if(board[r][c]=='.'){                    //do nothing                    }                else if(colSet.contains(board[r][c]))                    return false;                else                    colSet.add(board[r][c]);            }                                        //check for RowSet        HashSet<Character> rowSet=new HashSet<Character>();        for(int c=0;c<9;c++)            for(int r=0;r<9;r++){                if(board[r][c]=='.'){                    //do nothing                    }                else if(rowSet.contains(board[r][c]))                    return false;                else                    rowSet.add(board[r][c]);            }        return true;    }}



0 0
原创粉丝点击