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) 开 ...
随机推荐
- 必做课下作业MyCP
20175227张雪莹 2018-2019-2 <Java程序设计> 必做课下作业MyCP 要求 编写MyCP.java 实现类似Linux下cp XXX1 XXX2的功能,要求MyCP支 ...
- 安装部署Kafka集群
kafka是一个开源的分布式消息订阅系统(消息中间件) 安装过程 1.下载kafka_2.11-0.10.1.0.gz(ps:千万不要下错了,博主就是下到了src文件上去了,kafka中的zookee ...
- dubbo 调用服务超时
先贴出错误报告: Failed to invoke the method *** in the service ***. Tried times of the providers [] (/) on ...
- 排错技能:任务管理器中追踪某w3wp.exe是哪个IIS站点的application pool
如果Windows的任务管理器中发现某个w3wp.exe占用了100%CPU,那我们就要揪出这是那个网站的application pool在作怪, 首先,每个站点一定要单独使用各自的applicati ...
- Vue 动态路由传值
一.动态路由传值 1.配置动态路由: const routes = [ //动态路由路径参数以:开头 { path: '/Content/:aid', component:Content}, ] 2. ...
- oracle入坑日记<四>表空间
1 表空间是什么 1.1.数据表看做的货品,表空间就是存放货品的仓库.SQLserver 用户可以把表空间看做 SQLserver 中的数据库. 1.2.引用[日记二]的总结来解释表空间. 一个数 ...
- 如何用Fiddler手机抓包
截获智能手机发出的HTTP包有什么用? 用处一: 手机软件程序员利用Fiddler,可以截获手机发出的HTTP包, 从而调试程序: 用处二: 软件测试人员用于测试智能手机上的软件: 用处三: 可以用来 ...
- ubuntu远程windows桌面
最近把系统装成了ubuntu,需要远程windows ,就用到了ubuntu的 rdesktop工具 1.先打开终端输入 rdesktop 看系统是否安装了 rdesktop 如果没有安装需要 ...
- 6.3 基于二分搜索树、链表的实现的集合Set复杂度分析
两种集合类的复杂度分析 在[6.1]节与[6.2]节中分别以二分搜索树和链表作为底层实现了集合Set,在本节就两种集合类的复杂度分析进行分析:测试内容:6.1节与6.2节中使用的书籍.测试方法:测试两 ...
- part1
一.hello world 明确的指出 hello.py 脚本由 python 解释器来执行.coding:utf-8处理脚本中的中文 #!/usr/bin/env python # _*_ codi ...