leetcode刷题记录--Judge Route Circle

leetcode刷题记录--Judge Route Circle_第1张图片

题目

难度: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)