Javascript solution


  • 0
    H
    /**
     * @param {string} moves
     * @return {boolean}
     */
    var judgeCircle = function(moves) {
        var length = moves.length;
        var i = 0;
        var x = 0, y = 0;
        for(;i < length; i++) {
            var charac = moves.charAt(i);
            switch(charac) {
                case 'R':
                    x = x + 1;
                    break;
                case 'L':
                    x = x - 1;
                    break;
                case 'U':
                    y = y - 1;
                    break;
                case 'D':
                    y = y + 1;
                    break;
                default:
                    break;
            }
        }
        if(x == 0 && y == 0) {
            return true;
        } 
        return false;
    };
    

  • 0
    T
    This post is deleted!

Log in to reply
 

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