Valid sudoku: Time Limit Exceeded


  • 1
    P

    Here is my answer to valid sudoku problem, but it gets time limit exceeded error. Could someone help me to understand where is my problem please? Thanks.

    public class Solution {
        public boolean isValidSudoku(char[][] board) {
            Set singleSet = new HashSet<>();
            for (int i=0; i<9; i++)
            {
                singleSet.clear();
                for (int j=0; j<9; j++)
                {
                    if ((board[i][j]!='.') && (!singleSet.add(board[i][j])))
                        return false;
                }
                singleSet.clear();
                for (int j=0; j<9; j++)
                {
                    if ((board[j][i]!='.') && (!singleSet.add(board[j][i])))
                        return false;
                }
                singleSet.clear();
                for (int j=0; j<9; j++)
                {
                    if ((board[i/3*3+j/3][i%3*3+j%3]!='.') && (!singleSet.add(board[i/3*3+j/3][i%3*3+j%3])))
                        return false;
                }
            }
            return true;
        }
    }

  • 0

    That doesn't exceed the time limit. Not even close. Are you sure that that's the code? And that it was Time Limit Exceeded, not Timeout?


  • 0
    P

    Just tried to submit it again with the same answer and it is accepted...


Log in to reply
 

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