[JavaScript] code with object


  • 1
    /**
     * @param {string} moves
     * @return {boolean}
     */
    var judgeCircle = function(moves) {
        var position = [0, 0]
        // {index, +-1}
        var rlud = {
            "R": {
                "index": 0,
                "dir": 1
            },
            "L": {
                "index": 0,
                "dir": -1
            },
            "U": {
                "index": 1,
                "dir": 1
            },
            "D": {
                "index": 1,
                "dir": -1
            },
        }
        
        for(var i = 0; i < moves.length; i++) {
            position[rlud[moves[i]]["index"]] += rlud[moves[i]]["dir"]
        }
        
        if(position[0] == 0 && position[1] == 0) {
            return true
        }
        return false
    };
    
    

Log in to reply
 

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