1. 程式人生 > >[leetcode]36. Valid Sudoku

[leetcode]36. Valid Sudoku

Solution 1:用set的簡單迴圈

第一反應是set,set不能存重複的元素

Runtime: 21 ms, faster than 57.14% of Java online submissions for Valid Sudoku.

class Solution {
    public boolean isValidSudoku(char[][] board) {
        
        for(int i=0;i<9;i+=3){
            
            for(int j=0;j<9;j+=3){

                if(!CheckSquare(board,i,j)){
                    return false;
                }
            }
            
        }
        
         for(int i=0;i<9;i++){
            
                if(!CheckRow(board,i)){
                    return false;
                }
         }
        
         for(int j=0;j<9;j++){
            
                if(!CheckColumn(board,j)){
                    return false;
                }
         }   
        
        
        return true;
            
        }
    
        
    
    

  
    private boolean CheckRow(char[][] board,int i){
        Set<Character> set=new HashSet<>();
        
        for(int j=0;j<9;j++){
            
            if(board[i][j]!='.'){
                if(!set.contains(board[i][j])){
                    set.add(board[i][j]);
                }
                else{
                    return false;
                }                
            }

        }
        
        return true;
    }


    private boolean CheckColumn(char[][] board,int j){
        Set<Character> set=new HashSet<>();
        
        for(int i=0;i<9;i++){
            
            if(board[i][j]!='.'){
                if(!set.contains(board[i][j])){
                    set.add(board[i][j]);
                }
                else{
                    return false;
                }                
            }

        }
        
        return true;
    }



    private boolean CheckSquare(char[][] board,int i,int j){
        Set<Character> set=new HashSet<>();
        int p=i,q=j;
        for(p=i;p<i+3;p++){
            for(q=j;q<j+3;q++){
                
                if(board[p][q]!='.'){
                     if(!set.contains(board[p][q])){
                        set.add(board[p][q]);
                      }
                       else{
                           
                           
                        return false;
                      }                   
                }
                
            }
        }
        
        return true;
    } 
}

Solution 2:solution 1的再精簡版本,把所有check函式整合成一個

public class Solution {
public boolean isValidSudoku(char[][] board) {
    for (int i=0; i<9; i++) {
        if (!isParticallyValid(board,i,0,i,8)) return false;
        if (!isParticallyValid(board,0,i,8,i)) return false;
    }
    for (int i=0;i<3;i++){
        for(int j=0;j<3;j++){
            if (!isParticallyValid(board,i*3,j*3,i*3+2,j*3+2)) return false;
        }
    }
    return true;
}
private boolean isParticallyValid(char[][] board, int x1, int y1,int x2,int y2){
    Set singleSet = new HashSet();
    for (int i= x1; i<=x2; i++){
        for (int j=y1;j<=y2; j++){
            if (board[i][j]!='.') if(!singleSet.add(board[i][j])) return false;
        }
    }
    return true;
}
}

Solution 3: lc上genius!的辦法,堅持了

public boolean isValidSudoku(char[][] board) {
    Set seen = new HashSet();
    for (int i=0; i<9; ++i) {
        for (int j=0; j<9; ++j) {
            char number = board[i][j];
            if (number != '.')
                if (!seen.add(number + " in row " + i) ||
                    !seen.add(number + " in column " + j) ||
                    !seen.add(number + " in block " + i/3 + "-" + j/3))
                    return false;
        }
    }
    return true;
}