美文网首页
leetcode刷题记录--Judge Route Circle

leetcode刷题记录--Judge Route Circle

作者: fishliu | 来源:发表于2018-01-05 13:42 被阅读0次

    题目

    难度:easy

    Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot makes a circle, which means it moves back to the original place.
    The move sequence is represented by a string. And each move is represent by a character. The valid robot moves are R (Right), L (Left), U (Up) and D (down). The output should be true or false representing whether the robot makes a circle.

    Example 1:

    Input: "UD"
    Output: true
    

    Example 2:

    Input: "LL"
    Output: false
    

    第一次解法

    /**
     * @param {string} moves
     * @return {boolean}
     */
    var judgeCircle = function(moves) {
        let lNum = 0;
        let rNum = 0;
        let uNum = 0;
        let dNum = 0;
        moves.split('').filter((char)=>{
            switch (char)
            {
                case "L":
                    return lNum++
                    break
                case "R":
                    return rNum++
                    break
                case "U":
                    return uNum++
                    break
                case "D":
                    return dNum++
                    break
            }
    
        })
        if(lNum === rNum && uNum === dNum){
            return true
        }else{
            return false
        }
    };
    # runtime : 82ms
    

    相关文章

      网友评论

          本文标题:leetcode刷题记录--Judge Route Circle

          本文链接:https://www.haomeiwen.com/subject/kpzjnxtx.html