题目要求

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

LeetCode 657 Robot Return to Origin 解题报告的更多相关文章

  1. 【LeetCode】657. Robot Return to Origin 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 复数求和 counter统计次数 相似题目 参考资料 ...

  2. LeetCode 657. Robot Return to Origin

    There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of its mov ...

  3. LeetCode 657. Robot Return to Origin (C++)

    题目: There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of its ...

  4. LeetCode #657. Robot Return to Origin 机器人能否返回原点

    https://leetcode-cn.com/problems/robot-return-to-origin/ 设置 flagUD 记录机器人相对于原点在纵向上的最终位置 flagRL 记录机器人相 ...

  5. 【leetcode】657. Robot Return to Origin

    Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...

  6. 【Leetcode_easy】657. Robot Return to Origin

    problem 657. Robot Return to Origin 题意: solution1: class Solution { public: bool judgeCircle(string ...

  7. 657. Robot Return to Origin

    Description There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequenc ...

  8. LeetCode--Squares of a Sorted Array && Robot Return to Origin (Easy)

    977. Squares of a Sorted Array (Easy)# Given an array of integers A sorted in non-decreasing order, ...

  9. 【LeetCode】760. Find Anagram Mappings 解题报告

    [LeetCode]760. Find Anagram Mappings 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/find ...

随机推荐

  1. CentOS7安装RabbitMQ

    1.先安装Erlang rpm -Uvh http://www.rabbitmq.com/releases/erlang/erlang-18.1-1.el7.centos.x86_64.rpm 2.安 ...

  2. 看雪CTF第十五题

    1.直接运行起来,再用OD附加 在此处luajit加载并调用main函数 004021C7 E8 64FE0000 call CrackMe. ; luaL_newstate 004021CC 8BF ...

  3. UML类建模(强烈推荐-思路很清晰)

    UML类建模(强烈推荐-思路很清晰) 2016年10月23日 15:17:47 mbshqqb 阅读数:2315 标签: uml面向对象设计模式 更多 个人分类: 面向对象程序设计   UML的构造快 ...

  4. Java知多少(48)try语句的嵌套

    Try语句可以被嵌套.也就是说,一个try语句可以在另一个try块内部.每次进入try语句,异常的前后关系都会被推入堆栈.如果一个内部的try语句不含特殊异常的catch处理程序,堆栈将弹出,下一个t ...

  5. JavaScript高级用法三之浏览器对象

    综述 本篇的主要内容来自慕课网,内置对象,主要内容如下 1 window对象 2 JavaScript 计时器 3 计时器setInterval() 4 取消计时器clearInterval() 5 ...

  6. Matlab如何循环读取文件

    循环读取图片第一种方法①List =dir('*.jpg'); %如需其它图片格式支持,可以自己[重载dir()]函数,实现查找所有图片文件的功能,%如果图片是其它路径,可以用 ["路径&q ...

  7. Secure backup

    Secure backup 安全备份软件 安全备份软件致力于提供一款开源免费的安全云备份软件,支持文件管理,文件自动同步到云盘,增量备份等功能. 目前正在开发过程中...

  8. EF5+MVC4系列(12) 在主视图中直接用RenderAction调用子Action,并返回视图(Return View)或者分部视图(Return PartialView); 从主Action传值到子Action使用TempData传值;TempData高级用法

    结论: ViewData 适用于 在一次请求中 传递数据  . 比如我们从 主Action 到 主视图, 然后在 主视图中  用 RenderAction 请求子Action的时候,就是算作 一次请求 ...

  9. UIInterfaceOrientation over iOS6 (应用旋转屏幕)

      typedef NS_ENUM(NSInteger, UIInterfaceOrientation) { UIInterfaceOrientationUnknown = UIDeviceOrien ...

  10. SpringSecurity兑现多登录成功页面和登录成功返回被拦截界面

    SpringSecurity实现多登录成功页面和登录成功返回被拦截界面 使用SrpingSceurity作为认证和授权的安全框架可以省下很多基础工作. 具体可以参考SpringSecurity,这里不 ...