C# solution simple way using no collection


  • 0
    F
    public class Solution {
        public bool IsValidSudoku(char[,] board) {
            for(int i=0;i<9;i++){
                for(int j=0;j<9;j++){
                    if(board[i,j]!='.')
                        if(BreakRule(board, i,j)) return false;
                }
            }
            return true;
        }
        private bool BreakRule(char[,] board, int i, int j){
            
            for(int x=0;x<9;x++){
                //row
                if(x!=j&&board[i,x]==board[i,j])
                    return true;
                //column
                if(x!=i&&board[x,j]==board[i,j])
                    return true;
            }
            
            //sub square
            int x1=i/3, y1=j/3;
            for(int x=x1*3;x<(x1*3+3);x++){
                for(int y = y1*3; y<(y1*3+3);y++){
                    if(x!=i&&y!=j&&board[x,y]==board[i,j])
                        return true;
                }
            }
            
            return false;
        }
    }
    

Log in to reply
 

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