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.
Example 1:
Input: "UD"
Output: true
Explanation: The robot moves up once, and then down once. All moves have the same magnitude, so it ended up at the origin
where it started. Therefore, we return true.
Example 2:
Input: "LL"
Output: false
Explanation: The robot moves left twice. It ends up two "moves" to the left of the origin. We return false because i
Python3
1 class Solution:
2 def judgeCircle(self, moves):
3 """
4 :type moves: str
5 :rtype: bool
6 """
7 x = 0
8 y = 0
9 for s in moves:
10 if s == 'U':
11 y += 1
12 elif s == 'D':
13 y -= 1
14 elif s == 'R':
15 x += 1
16 elif s == 'L':
17 x -= 1
18 if (x, y) == (0, 0):
19 return True
20 else:
21 return False
Robot Return to Origin的更多相关文章
- 【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 ...
- 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, ...
- [Swift]LeetCode657. 机器人能否返回原点 | 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
There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of its mov ...
- 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 657 Robot Return to Origin 解题报告
题目要求 There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of it ...
- 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 ...
- C#LeetCode刷题之#657-机器人能否返回原点(Robot Return to Origin)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3959 访问. 在二维平面上,有一个机器人从原点 (0, 0) 开 ...
随机推荐
- xyplorer设置备忘
xyplorer设置备忘https://www.cnblogs.com/liuzhaoyzz/p/9911665.html1.双击向上一级工具→配置→菜单,鼠标,安全性→鼠标→双击空白空间时向上一级. ...
- ORC 文件存储格式
1.orc列式存储概念 a)列式存储:orc并不是纯粹的列式存储,也是先基于行对数据表进行分组(行组),然后对行组进行列式存储. b)查询数据的时候不需要扫描全部数据(磁盘IO),只需查询指定列即可. ...
- 报错:Flink Could not resolve substitution to a value: ${akka.stream.materializer}
报错现象: Exception in thread "main" com.typesafe.config.ConfigException$UnresolvedSubstitutio ...
- nvidia驱动自动更新版本后问题解决 -- failed to initialize nvml: driver/library version mismatch
因为必须关闭桌面窗口, 建议另外一台电脑ssh连接操作 1. 卸载旧版本并关闭图形界面 sudo apt-get remove --purge nvidia-\* sudo service light ...
- 老集群RAC双网卡绑定
老集群RAC双网卡绑定 作者:Eric 微信:loveoracle11g [root@db-rac02 network-scripts]# cat ifcfg-bond0 DEVICE=bond0 I ...
- [WebService].net中WebService的使用实例
.net中WebService的使用实例 一.创建一个Webwebservice 1.新建一个项目WebserverDemo 2.在项目处添加新建项,添加一个web服务 3.编辑TestServer. ...
- 学习MeteoInfo二次开发教程(十二)
1.添加新的Form窗体: 在解决方案资源管理器中,右键MeteoInfoDemo,“添加”,“Windows 窗体” 2.新窗体中添加好layersLegend1和Layout之后,要把layers ...
- WordPress版微信小程序3.5版发布
最近花时间对WordPress版微信小程序做了一些完善和调整,修复不少程序的问题.一个程序的完善是持续和渐进的,没有最好,只有更完善.虽然会采纳一些用户的建议和意见,但我会从一个产品角度去考虑,哪些功 ...
- RN-进阶
ActivityIndicator 显示一个圆形的loading的提示符 react-native-tab-navigator npm install react-native-tab-navigat ...
- flask同源策略解决办法及flask-cors只允许特定域名跨域
falsk 同源策略解决办法: 使用 flask-cors 包 并且 在代码里 加响应的一行代码解决. from flask import Flask, session from flask_cors ...