There seem a bug for JavaScript


  • 0
    E

    I ran the following code in playground and everything worked fine:

    var data = {};
    
    var solve = function(row, column, maxRow, maxColumn, board, picked) {
        if (row > maxRow) return true;
        
        if (column === 0) {
            picked = new Set();
        }
    
        if (column > maxColumn) return getFromIntersection(row + 1, 0, maxRow, maxColumn, board, picked);
        if (board[row][column] !== '.') return getFromIntersection(row, column + 1, maxRow, maxColumn, board, picked);
    
        var rowId = `r${row + 1}`;
        var columnId = `c${column + 1}`;
    
        var boxRow = Math.floor(row / 3) + 1;
        var boxColumn = Math.floor(column / 3) + 1;
        var boxId = `b${(boxRow - 1) * 3 + boxColumn}`;
    
        var result = false;
        var rowSet = data[rowId];
        var columnSet = data[columnId];
        var boxSet = data[boxId];
    
        for (var val of rowSet.keys()) {
            if (picked.has(val)) continue;
            if (columnSet.has(val) && boxSet.has(val)) {
                picked.add(val);
                columnSet.delete(val);
                boxSet.delete(val);
                board[row][column] = val;
                if (getFromIntersection(row, column + 1, maxRow, maxColumn, board, picked)) {
                    result = true;
                    break;
                } else {
                    picked.delete(val);
                    columnSet.add(val);
                    boxSet.add(val);
                    board[row][column] = '.';
                }
            }
        }
    
        return result;
    };
    
    var solveSudoku = function(board) {
        console.log(board);
        for (var i = 0; i < board.length; i += 1) {
            var box = board[i];
            
            var rowId = `r${i + 1}`;
            var boxRow = Math.floor(i / 3) + 1;
    
            var rowOffset = Math.floor(i / 3) * 3;
            var colOffset = (i % 3) * 3;
            for (var j = 0; j < box.length; j += 1) {
                
                var columnId = `c${j + 1}`;
                var boxColumn = Math.floor(j / 3) + 1;
                var boxId = `b${(boxRow - 1) * 3 + boxColumn}`;
                
                if (!data[boxId]) {
                    data[boxId] = new Set(['1', '2', '3', '4', '5', '6', '7', '8', '9']);
                }
                if (!data[columnId]) {
                    data[columnId] = new Set(['1', '2', '3', '4', '5', '6', '7', '8', '9']);
                }
                if (!data[rowId]) {
                    data[rowId] = new Set(['1', '2', '3', '4', '5', '6', '7', '8', '9']);
                }
                var num = box[j];
                if (num === '.') continue;
                
                data[columnId].delete(num);
                data[rowId].delete(num);
                data[boxId].delete(num);
            }
        }
        
        solve(
            0,
            0,
            board.length - 1,
            board[0].length - 1,
            board
        );
        console.log(board);
    };
    

    But it broken when I try to submit this solution, the error message is Wrong Answer, cause it didn't change the value in board.

    Please, help me to find where the problem is. Is there a bug in Leetcode editor (May be cause javascript version)? Thanks.


Log in to reply
 

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