LeetCode2.1.14 @ Valid Sudoku 验证数独D2F2

本文介绍了一种使用HashMap来检查数独盘面有效性的方法,确保每一行、每一列及每一个小九宫格内的数字都不重复。通过具体的Java代码实现展示了如何进行验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值