题目要求

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. idea 自动根据屏幕代码换行

    勾上即可 -- 建议不开启 有些时候从其他来源网站直接copy的代码文件会出现不勾选还会出现自动换行的情况 解决方法  新建文件  代码全部选剪过去

  2. Qt动态库静态库的创建、使用、多级库依赖、动态库改成静态库等详细说明

    本文描述的是windows系统下,通过qtcreator在pro文件中添加动态库与静态库的方法: 1.添加动态库(直接添加动态库文件.dll,非子项目) 通过qtcreator创建动态库的方法就不在此 ...

  3. An SPI class of type org.apache.lucene.codecs.PostingsFormat with name 'Lucene50' does not exist. You need to add the corresponding JAR file supporting this SPI to your classpath. The current classp

    背景介绍: 当ES中guava库与hive等组件的库冲突时,对Elasticsearch库进行shade,relocate解决库冲突问题. 当使用"org.apache.maven.plug ...

  4. WAS生成的文件:javacore.***.txt 、heapdump.***.phd、core.***.dmp、Snap.***.trc

    WAS生成的常见文件有哪些? 原文链接:http://blog.csdn.net/pqh20085101092/article/details/39370389 javacore.***.txt : ...

  5. SpringBoot 拦截器(Interceptor)的使用

    拦截器intercprot  和 过滤器 Filter 其实作用类似 在最开始接触java 使用struts2的时候,里面都是filter 后来springmvc时就用interceptor 没太在意 ...

  6. [OpenCV] Samples 17: Floodfill

    一种聚类方式,代码解析 #!/usr/bin/env python ''' Floodfill sample. Usage: floodfill.py [<image>] Click on ...

  7. 【代码审计】XYHCMS V3.5代码执行漏洞分析

      0x00 环境准备 XYHCMS官网:http://www.xyhcms.com/ 网站源码版本:XYHCMS V3.5(2017-12-04 更新) 程序源码下载:http://www.xyhc ...

  8. umi怎么去添加配置式路由

    今天在学习umi,他的路由机制非常的方便,但是在学到配置式路由的时候,看官方文档里面一笔带过: 对于我这种小萌新来说,有点懵,我需要把配置文件放到哪里呢?经过一番研究,发现它是放在根目录的.umirc ...

  9. Phoenix系列:原子的Upsert

    Phoenix的插入语句是Upsert,Update和Insert的组合语义.即,如果数据表中没有这条记录那么插入这条记录,如果有则更新.判断是否存在相同的数据是使用ON DUPLICATE KEY来 ...

  10. iOS - Xcode项目统计总代码行数

    最新公司需要把项目代码量统计一下,第一时间找到Xcode插件管理工具Alcatraz,查找插件ZLXCodeLine,这是一个快速统计Xcode工程项目代码量的插件,好像已经不支持Alcatraz安装 ...