Clean Java solution


  • 0
    J
    int SUDOKU_LEN = board.length;
            
    int[] cacheX = new int[SUDOKU_LEN + 1];
    int[] cacheY = new int[SUDOKU_LEN + 1];
    int[] cacheS = new int[SUDOKU_LEN + 1];
    
    char cX, cY, cS;
    for (int i = 0; i < SUDOKU_LEN; i++) {
        for (int j = 0; j < SUDOKU_LEN; j++) {
            cX = board[i][j];
            cY = board[j][i];
            cS = board[(i / 3) * 3 + (j / 3)]
                      [(i * 3) % 9 + (j % 3)];
    
            if (cX != '.' && 0 != cacheX[cX - '0']++) return false;
            if (cY != '.' && 0 != cacheY[cY - '0']++) return false;
            if (cS != '.' && 0 != cacheS[cS - '0']++) return false;
        }
        
        cacheX = new int[SUDOKU_LEN + 1];
        cacheY = new int[SUDOKU_LEN + 1];
        cacheS = new int[SUDOKU_LEN + 1];
    }
    
    return true;

Log in to reply
 

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