Not concise but easy to understand java solution


  • 0
    Y
    public class Solution {
        public boolean isValidSudoku(char[][] board) {
            ArrayList<Character> charList = new ArrayList<Character>();
            for (int i=0;i<9;i++){
                charList.clear();
                for (int j=0;j<9;j++){
                    if (board[i][j]!='.'){
                        if (charList.contains(board[i][j])) return false;
                        else charList.add(board[i][j]);
                    }
                }
            }
            for (int j=0;j<9;j++){
                charList.clear();
                for (int i=0;i<9;i++){
                    if (board[i][j]!='.'){
                        if (charList.contains(board[i][j])) return false;
                        else charList.add(board[i][j]);
                    }
                }
            }
            for (int m=0;m<3;m++){
                for (int n=0;n<3;n++){
                    charList.clear();
                    for (int i=0;i<3;i++){
                        for (int j=0;j<3;j++){
                            if (board[m*3+i][n*3+j]!='.'){
                            if (charList.contains(board[m*3+i][n*3+j])) return false;
                            else charList.add(board[m*3+i][n*3+j]);
                            }
                        }
                    }
                }
            }
            return true;
        }
    }
    

Log in to reply
 

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