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 ...
随机推荐
- Linux里的2>&1究竟是什么
我们在Linux下经常会碰到nohup command>/dev/null 2>&1 &这样形式的命令.首先我们把这条命令大概分解下首先就是一个nohup表示当前用户和系统 ...
- Ubuntu 14.04 配置VNC服务 配置Xfce4桌面
一.安装配置VNC 1.首先安装VNC apt-get install vnc4server 2.为VNC设置密码 vncpasswd 输入密码,然后再确认一遍,就OK了. 3.启动VNC vncse ...
- .bat文件调用java类的main方法
此处记录一个小例子,备用,说不定哪天写小工具时会用到. @echo on set JAVA_HOME=C:\Program Files\Java\jdk1.6.0_43 set classpath=. ...
- JdbcTemplate in()传参
1. 实体类 import java.util.List; public class Param { private List<String> names; private List< ...
- Qt编写GIF录屏工具(开源)
在平时的写作过程中,经常需要将一些操作动作和效果图截图成gif格式,使得涵盖的信息更全面更生动,有时候可以将整个操作过程和运行效果录制成MP4,但是文件体积比较大,而且很多网站不便于上传,基本上都支持 ...
- vs2017更新后web项目部分后台代码类没有颜色,也没有自动提示输入功能
vs2017有的版本更新后默认.net framework框架是.net framework4.6.1,将项目的.net framework框架更改为4.6.1,颜色和自动提示出现
- day_6.10 tcp三次握手 四次挥手
tcp和udp对比 tcp比udp稳定 断开连接的四次挥手
- ssh文件配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- ssh 管理 linux登录远程服务器
使用 ssh 免秘登录方式 客户端:1. 生成公钥和私钥 ssh-keygen 一般不需要对私钥设置口令(passphrase),如果担心私钥的安全,这里可以设置一个. 运行结束以后,在$HOME/. ...
- ThinkPHP框架 做个简单表单 添加数据例子__ACTION__ __SELF__
public function zhuCe(){//自定义zhuCe方法和zhuCe显示表里的__ACTiON__这个相互交接 //实现两个逻辑 //1,显示注册页面 //2.向数据库添加内容 //自 ...