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) 开 ...
随机推荐
- JavaScript BOM和DOM
Browser Object Model BOM是所有JavaScript的核心,所有的功能其实都建立在BOM基础之上.各浏览器提供的BOM的功能存在很大差异,BOM在HTML5中已经有很大一部分被放 ...
- oracle over 函数几个例子
测试使用的数据为scott/tiger模式下的emp表: 我们使用JOB和SAL这两个列测试: 上面语句指按照职业JOB分组(partition by job)然后在每个分组内,按照薪水(sal)进行 ...
- C# 6.0:新的Dictionary Initializer
初始化Dictionary不是什么新东西,你可以简单的通过Collection Initializer来初始化一个Dictionary,这是从C#3.0就有的特性.Collection Initial ...
- monodepth 训练记录
2019年2月22日13:52:37 https://zhuanlan.zhihu.com/p/29968267 这里有个tensorlfow代码的阅读博客: https://zhuanlan.zhi ...
- 软件推荐----数据库数据处理Navicat Premium
一般情况下,企业对数据库的管控很严,通常管控分成以下: 对整台服务器管理权限,如:操作系统Administrator+SA 对单个数据库(Oracle的表空间)完全权限,如:SQL DB_Owner ...
- (转)并发编程 – Concurrent 用户指南
原文出处: 高广超 译序 本指南根据 Jakob Jenkov 最新博客翻译,请随时关注博客更新:http://tutorials.jenkov.com/java-util-concurrent/in ...
- JAVA相关技术
开发服务器环境: 1.Linux系统 CentOS 6.5\7 2.JDK1.8 3.tomcat 9 4.mysql 5.7 开发环境: 1.开发集成工具:idea 2.构建工具maven 仓库暂时 ...
- MySql查询问题select from
一开始这样不行,后来把值用单引号引起来就行了SELECT * FROM reflectmastercore WHERE name=free 就像下面这样 SELECT * FROM reflectma ...
- Oracle不常用SQL
Oracle 查询最近创建的表 select * from user_objects where object_type='TABLE' order by created desc Oracle 查询 ...
- swagger支持中英文
内存中使用一种数据结构存储着大量的信息,其中原始的描述信息(各种属性的描述,种类很多),中英文都包含,格式为:"english//中文".需求:描述description需要动态变 ...