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 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 解题报告的更多相关文章
- 【LeetCode】657. Robot Return to Origin 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 复数求和 counter统计次数 相似题目 参考资料 ...
- 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 ...
- 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 ...
- LeetCode #657. Robot Return to Origin 机器人能否返回原点
https://leetcode-cn.com/problems/robot-return-to-origin/ 设置 flagUD 记录机器人相对于原点在纵向上的最终位置 flagRL 记录机器人相 ...
- 【leetcode】657. Robot Return to Origin
Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...
- 【Leetcode_easy】657. Robot Return to Origin
problem 657. Robot Return to Origin 题意: solution1: class Solution { public: bool judgeCircle(string ...
- 657. Robot Return to Origin
Description There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequenc ...
- 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, ...
- 【LeetCode】760. Find Anagram Mappings 解题报告
[LeetCode]760. Find Anagram Mappings 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/find ...
随机推荐
- 3D 特征点概述(2)
还是紧接着上一文章的思路继续介绍3D特征点的基本概念问题,还是这个表格: Feature Name Supports Texture / Color Local / Global / Regional ...
- Java知多少(94)键盘事件
键盘事件的事件源一般丐组件相关,当一个组件处于激活状态时,按下.释放或敲击键盘上的某个键时就会发生键盘事件.键盘事件的接口是KeyListener,注册键盘事件监视器的方法是addKeyListene ...
- MVC的Membership
摘自:http://stackoverflow.com/a/16734651/1616023 See the summaries below each quote for a quick answer ...
- 大量删除MySQL中的数据
出现的背景: 公司做了一个redis相关的项目,其中mysql存储了很多统计数据.比如客户端上报的数据,redis实例的数据,应用的数据,机器的数据等.每天都在上报,采集,由于没有定期删除,数据大量累 ...
- MTK 修改默认屏幕亮度
frameworks\base\packages\SettingsProvider\res\values\defaults.xml <!-- Default screenbrightness, ...
- 5. RAMN备份与恢复
一. rman简介 RMAN(Recovery Manager)是一种用于备份(backup).还原(restore)和恢复(recover)数据库的 Oracle 工具.RMAN只能用于ORACLE ...
- 如何获取控件id,包名,类名
- mysql基础---->mybatis的批量插入(一)
这里面记录一下使用mybatis处理mysql的批量插入的问题,测试有可能不准.只愿世间风景千般万般熙攘过后,字里行间,人我两忘,相对无言. mybatis的批量插入 我们的测试主体类是springb ...
- 推荐一款好用的文件/文件夹对比工具 —— Beyond Compare
推荐一款好用的文件/文件夹对比工具 —— Beyond Compare! 有需要的人,用了都说好: 不知道这个是干嘛用的,说再多也没用.
- 欢迎访问新博客(pfzheng.tech)
这两天折腾了几天的服务器,搞了一个临时的个人博客. 最先入手的域名pfzheng.tech,但是发现竟然不支持备案.天哪,我做错了什么,只好再买域名.新域名pfzheng.cn正在备案中. 新博客基于 ...