leetcode刷题记录--Judge Route Circle

题目

难度: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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 今天小雨。 前段时间,杭州,峰会,突然有了难得的蓝天,然后所有人举着手机对着天空,节日一样狂欢,抽疯似的刷屏,还有...
    海上散人阅读 1,391评论 0 0
  • 前段时间,阿里巴巴技术团队公开了内部的编程规范 ----《阿里巴巴 Java 开发手册》,旨在规范开发标准,提高协...
    落英坠露阅读 4,434评论 0 2
  • 耳目一些的非好莱坞魔法世界。
    文嗔阅读 2,675评论 0 0