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 ...
随机推荐
- Visual自动添加CSS兼容前缀
安装方法 打开vs code 的 扩展 ---> 搜索 Autoprefixer,并安装. 使用方法 打开css文件,按F1,选择 Autoprefix CSS 这条命令 没执行命令之前: 执行 ...
- WPF Input Validation Using MVVM
Data validation is a key part in WPF.Validation is used to alert the user that the data he entered i ...
- 第四百节,Django+Xadmin打造上线标准的在线教育平台—生产环境部署CentOS6.5安装python3.5.1
第四百节,Django+Xadmin打造上线标准的在线教育平台—生产环境部署CentOS6.5安装python3.5.1 1.检查系统是否安装了python [root@192 ~]# rpm -qa ...
- 性能优化系列八:MYSQL的配置优化
一.关键配置 1. 配置文件的位置 MySQL配置文件 /etc/my.cnf 或者 /etc/my.cnf.d/server.cnf 几个关键的文件:.pid文件,记录了进程id.sock文件,是内 ...
- .NET中进行Base64加密解密
方法一: /// <summary> /// Base64加密 /// </summary> /// <param name="Message"> ...
- Android样式的开发:selector篇
上一篇详细讲了shape的用法,讲解了怎么用shape自定义矩形.圆形.线形和环形,以及有哪些需要注意的地方.不过,shape只能定义单一的形状,而实际应用中,很多地方比如按钮.Tab.ListIte ...
- Window应急响应(一):FTP暴力破解
0x00 前言 FTP是一个文件传输协议,用户通过FTP可从客户机程序向远程主机上传或下载文件,常用于网站代码维护.日常源码备份等.如果攻击者通过FTP匿名访问或者弱口令获取FTP权限,可直接上传 ...
- 【转载】Eclipse 的快捷键以及文档注释、多行注释的快捷键
一.多行注释快捷键 1.选中你要加注释的区域,用ctrl+shift+C 或者ctrl+/ 会加上//注释2.先把你要注释的东西选中,用shit+ctrl+/ 会加上/* */注释 3.以上快捷 ...
- Using Information Fragments to Answer the Questions Developers Ask
content : 1.采访了11个开发者,获得78个常问的问题:2.对78个问题进行分类,分为8类:These questions span eight domains of information ...
- Excel如何实现两个工作表数据的对比
https://jingyan.baidu.com/article/63f236281f17650208ab3d97.html Sub 数据对比() Dim i As Integer Dim j As ...