Can anyone review/feedback my Clean Java accepted code.


  • 0
    A
    public class Solution {
        public boolean isValidSudoku(char[][] board) {
            HashSet<Character> set= new HashSet<>();
            int row=0;
            int col=0;
            for(int i=0;i<9;i++){
                //for rows
                for(int j=0;j<9;j++){
                    if(board[i][j]=='.')
                        continue;
                    if(set.contains(board[i][j]))
                        return false;
                    else
                        set.add(board[i][j]);                                
                    
                }
                set.clear();
                //for cols
                for(int k=0;k<9;k++){
                    if(board[k][i]=='.')
                        continue;
                    
                    if(set.contains(board[k][i]))
                        return false;
                    else
                        set.add(board[k][i]);
                    
                }
                set.clear();
                //for box
                row = i%3==0?i:row; 
                col = i%3==0?0:col+3;
                for(int r=row;r<row+3;r++){
                    for(int c=col;c<col+3;c++){
                        if(board[r][c]=='.')
                        continue;
                        if(set.contains(board[r][c]))
                            return false;
                        else
                            set.add(board[r][c]);
                    }
                }
                set.clear();
            }
            return true;
            
        }
    }

  • 0
    K

    I really like your codes, it is much readable. Somehow I just do not understand why people like to brag how they can use the least number of lines to get the result by integrating several lines into a complex line which other people need to read carefully to understand


  • 0
    C

    Can't agree more!!! I really hate to read those so-called "simple" code, it's just short but not simple.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.