成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

6leetcode機器移動

glumes / 2757人閱讀

1 題目

There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of its moves, judge if this robot ends up at (0, 0) after it completes its moves.

The move sequence is represented by a string, and the character moves[i] represents its ith move. Valid moves are R (right), L (left), U (up), and D (down). If the robot returns to the origin after it finishes all of its moves, return true. Otherwise, return false.

Note: The way that the robot is "facing" is irrelevant. "R" will always make the robot move to the right once, "L" will always make it move left, etc. Also, assume that the magnitude of the robot"s movement is the same for each move

## 2. 示例
Input: "UD"
Output: true 
Explanation: The robot moves up once, and then down once. All moves have the same magnitude, so it ended up at the origin where it started. Therefore, we return true
Input: "LL"
Output: false
Explanation: The robot moves left twice. It ends up two "moves" to the left of the origin. We return false because it is not at the origin at the end of its moves.
2. 解答
var judgeCircle = function(moves) {
    let x = 0, y = 0;
    for (let move of moves) {
        switch(move) {
            case "U": y++ ;break;
            case "D": y-- ;break;
            case "L": x-- ;break;
            case "R": x++ ;break;
        }
    }
    return x === 0 && y === 0
};
Runtime: 68 ms, faster than 86.60% of JavaScript online submissions
for Robot Return to Origin. Memory Usage: 36.7 MB, less than 40.71% of
JavaScript online submissions for Robot Return to Origin.
3. 其他解法
const judgeCircle = (moves) => {
    return moves.split("")
        .reduce((p, m) => [p[0] + (m === "R") - (m === "L"), p[1] + (m === "U") - (m === "D")], [0, 0])
        .join("") === "00"
};

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉載請注明本文地址:http://systransis.cn/yun/103156.html

相關文章

  • 80VPS:日本/香港CN2服務器600元/月,E5/16G/1TB/20M帶寬

    摘要:商家提供最多的還是和獨立服務器租用,之前部落主要分享他的主機,最近關注獨立服務器,比如中國香港,或者帶寬,可選防禦,現(xiàn)在系列香港或者日本服務器每月僅元起。本文於 2021-09-02 07:57 更新,部分內容具有時效性,如有失效,請留言 80VPS怎麼樣,80VPS好不好,80VPS是一家成立較早的綜合性網絡產品服務商,提供的產品包括域名註冊、虛擬主機、VPS、雲(yún)服務器和獨立服務器租...

    wuyangchun 評論0 收藏0
  • 修羅云:成立兩周年特惠,有深港IPCL、香港HKT、廣州、中山、徐州、杭州、佛山等,最低6折起

    摘要:修羅雲(yún)成立兩周年了風雨同舟,數(shù)次搬遷在此衷心感謝大家的陪伴官網優(yōu)惠碼首月折,續(xù)費折廣州,中山,徐州,杭州,佛山獨立可用。修羅雲(yún)怎麼樣,修羅雲(yún)好不好,修羅雲(yún)是一家國內老牌商家,商家以銷售NAT機器起家,國內的中轉機相當不錯,給的帶寬都非常高,如果你是移動或是教育網到國外速度非常慢,可以使用他家的中轉機拉一下,直接起飛的感覺,商家目前推出兩周年活動,他家一般很少有活動,本次優(yōu)惠為新購首月6折,續(xù)...

    Little_XM 評論0 收藏0
  • DMIT:國外高端VPS雲(yún)服務器特價優(yōu)惠,美國/香港/日本可選,高端CN2 GIA優(yōu)質線路大帶寬不限

    摘要:內存流量帶寬價格購買核不限月購買核不限月購買核不限月購買核不限月購買網絡測試查看含有美國洛杉磯標籤的文章美國洛杉磯查看含有洛杉磯標籤的文章洛杉磯高防日本東京查看含有香港標籤的文章香港 最近站長整理了一下DMIT商家的CN2 GIA特價優(yōu)惠VPS雲(yún)服務器產品,主要是美國、香港、日本等地區(qū)的VPS雲(yún)服務器產品,線路方面依然非常優(yōu)質,有需要國外優(yōu)質線路VPS雲(yún)服務器的朋友可以關注一...

    qqlcbb 評論0 收藏0

發(fā)表評論

0條評論

glumes

|高級講師

TA的文章

閱讀更多
最新活動
閱讀需要支付1元查看
<