【LeetCode】657. Judge Route Circle 解题报告

901 篇文章 208 订阅

【LeetCode】657. Judge Route Circle

标签(空格分隔): LeetCode


题目地址:https://leetcode.com/problems/judge-route-circle/description/

题目描述:

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

Ways

python里面表达坐标比较简单,直接用列表就可以。

这个题就是判断移动了几次之后是否在原来的位置,只要每步都去修改一次即可。

方法一,O(n):

class Solution:
    def judgeCircle(self, moves):
        """
        :type moves: str
        :rtype: bool
        """
        position = [0, 0]
        for move in moves:
            if move == 'R':
                position[0] += 1
            if move == 'L':
                position[0] -= 1
            if move == 'U':
                position[1] += 1
            if move == 'D':
                position[1] -= 1
        return position == [0, 0]

方法二,数左右移动的次数和上下移动的次数是否相等:

class Solution:
    def judgeCircle(self, moves):
        """
        :type moves: str
        :rtype: bool
        """
        return moves.count('L') == moves.count('R') and moves.count('U') == moves.count('D')

方法三,看了Discuss才知道,python原来也可以用复数!

class Solution:
    def judgeCircle(self, moves):
        """
        :type moves: str
        :rtype: bool
        """
        directs = {'L':-1, 'R':1, 'U':1j, 'D':-1j}
        return 0 == sum(directs[move] for move in moves)

Date

2018 年 1 月 13 日

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值