首页 > 其他分享 >leetcode-657. 机器人能否返回原点

leetcode-657. 机器人能否返回原点

时间:2023-01-05 19:13:16浏览次数:66  
标签:原点 机器人 else 657 vv moves leetcode

657. 机器人能否返回原点 - 力扣(Leetcode)

刚开始用了个 map ,比较复杂,后来看了答案,按照这种简单的方式,并且做了 len(moves) % 2 != 0 的判断

func judgeCircle(moves string) bool {
    x, y := 0, 0

    if len(moves) % 2 != 0 {
        return false
    }

    for _, v := range moves {
        vv := string(v)
        if vv == "R" {
            x++
        } else if vv == "L" {
            x--
        } else if vv == "U" {
            y++
        } else if vv == "D" {
            y--
        }
    }

    return x == 0 && y == 0
}

标签:原点,机器人,else,657,vv,moves,leetcode
From: https://www.cnblogs.com/wudanyang/p/17028642.html

相关文章