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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 今天小雨。 前段時(shí)間,杭州,峰會(huì),突然有了難得的藍(lán)天,然后所有人舉著手機(jī)對(duì)著天空,節(jié)日一樣狂歡,抽瘋似的刷屏,還有...
    海上散人閱讀 229評(píng)論 0 0
  • 前段時(shí)間,阿里巴巴技術(shù)團(tuán)隊(duì)公開了內(nèi)部的編程規(guī)范 ----《阿里巴巴 Java 開發(fā)手冊(cè)》,旨在規(guī)范開發(fā)標(biāo)準(zhǔn),提高協(xié)...
    落英墜露閱讀 1,040評(píng)論 0 2
  • 耳目一些的非好萊塢魔法世界。
    文嗔閱讀 399評(píng)論 0 0

友情鏈接更多精彩內(nèi)容