博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode 657 Robot Return to Origin 解题报告
阅读量:4981 次
发布时间:2019-06-12

本文共 1107 字,大约阅读时间需要 3 分钟。

题目要求

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.

题目分析及思路

题目给出一个机器人的移动序列,若能返回原点则返回true,否则返回false。可以设定水平和垂直两个方向上的计数,若移动之后仍保持方位的不变,则返回到了原点。

python代码​

class Solution:

    def judgeCircle(self, moves):

        """

        :type moves: str

        :rtype: bool

        """

        vertical = 0

        horizontal = 0

        for move in moves:

            if move == 'R':

                horizontal+=1

            elif move == 'L':

                horizontal-=1

            elif move == 'U':

                vertical+=1

            else:

                vertical-=1

        return horizontal == 0 and vertical == 0

            

        

 

转载于:https://www.cnblogs.com/yao1996/p/10237164.html

你可能感兴趣的文章
类对象
查看>>
[Voice communications] 声音的滤波
查看>>
软件建模——第9章 毕业论文管理系统—面向对象方法
查看>>
[SDOI2008]洞穴勘测
查看>>
Difference between Linearizability and Serializability
查看>>
IDEA使用操作文档
查看>>
UIView
查看>>
添加日期选择控件
查看>>
bzoj4765: 普通计算姬 (分块 && BIT)
查看>>
看完漫画秒懂区块链
查看>>
Oracle命令类别
查看>>
stc12c5a60s2驱动TEA5767收音机模块硬件调试总结
查看>>
vue中提示$index is not defined
查看>>
css选择器
查看>>
ASP.NET上传下载文件
查看>>
Galaxy Nexus 全屏显示-隐藏Navigation Bar
查看>>
Spring中使用Velocity模板
查看>>
上周热点回顾(8.18-8.24)
查看>>
Feature toggle
查看>>
day02
查看>>