C# Accepted solution with list


  • 1
    H
    public bool IsValidSudoku(char[,] board) 
        {
                List<char>[,] sqaureArray = new List<char>[3, 3];
                List<char>[] rowList = new List<char>[9];
                List<char>[] columnList = new List<char>[9];
    
                // Initialize every list in the multidimensional array
                for (int i = 0; i < sqaureArray.GetLength(0); i++)
                {
                    for (int j = 0; j < sqaureArray.GetLength(1); j++)
                    {
                        sqaureArray[i, j] = new List<char>();
                    }
                }
    
                // Initialize every list in the array
                for (int i = 0; i < rowList.GetLength(0); i++)
                {
                    rowList[i] = new List<char>();
                    columnList[i] = new List<char>();
                }
    
                // Check no repeat for each of the 3 lists
                for (int i = 0; i < board.GetLength(0); i++)
                {
                    for (int j = 0; j < board.GetLength(1); j++)
                    {
                        if (board[i, j] == '.')
                            continue;
    
                        // Check rows
                        if (rowList[i].Contains(board[i, j]))
                            return false;
                        rowList[i].Add(board[i, j]);
    
                        // Check columns
                        if (columnList[j].Contains(board[i, j]))
                            return false;
                        columnList[j].Add(board[i, j]);
    
                        // Check squares
                        if (sqaureArray[i/3, j/3].Contains(board[i, j]))
                            return false;
                        sqaureArray[i/3, j/3].Add(board[i, j]);
                    }
                }
    
                return true;
        }

Log in to reply
 

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