【LeetCode】657. Robot Return to Origin 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/robot-return-to-origin/description/
题目描述
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 it is not at the origin at the end of its moves.
题目大意
输入是机器人移动的方向,每次移动一步。问最终是否出现在出发位置。机器人的面向和移动方向是不想关的。
解题方法
复数求和
Python支持复数运算的,所以指定不同的方向是不同的实数数字就行了。复数的标记是j。最后求和判断是不是0就是到了原点。
时间复杂度是O(n),空间复杂度是O(1).
class Solution:
def judgeCircle(self, moves):
"""
:type moves: str
:rtype: bool
"""
directs = {'L':-1, 'R':1, 'U':1j, 'D':-1j}
return 0 == sum(directs[move] for move in moves)
counter统计次数
显而易见,回到原点的要求是向上走的次数和向下走的次数相等,并且向左和向右走的次数相等。直接字符串统计判断是否哦相等,很快就写出来。
时间复杂度是O(n),空间复杂度是O(1).
class Solution:
def judgeCircle(self, moves):
"""
:type moves: str
:rtype: bool
"""
count = collections.Counter(moves)
return count['U'] == count['D'] and count['L'] == count['R']
上面的代码也可以直接使用4个变量统计次数,但是时间并没有明显提升。
class Solution:
def judgeCircle(self, moves):
"""
:type moves: str
:rtype: bool
"""
u = d = l = r = 0
for move in moves:
if move == 'U':
u += 1
elif move == "D":
d += 1
elif move == 'L':
l += 1
elif move == 'R':
r += 1
return u == d and l == r
相似题目
参考资料
日期
2018 年 11 月 2 日 —— 浑浑噩噩的一天
【LeetCode】657. Robot Return to Origin 解题报告(Python)的更多相关文章
- 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
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 ...
- 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 [LeetCode] 题目地址:h ...
- 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】654. Maximum Binary Tree 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
随机推荐
- R语言与医学统计图形【2】散点图、盒形图
R语言基础绘图系统 基础图形--散点图.盒形图 plot是一个泛型函数(generic method),对于不同的数据绘制不同的图形. par函数的大部分参数在plot中通用. 1.散点图 plot绘 ...
- R语言与医学统计图形-【30】流行病学数据可视化
sjPlot包适用于社会科学.流行病学中调查数据可视化,且能和SPSS数据无缝对接(流行病学问卷调查录入Epidata软件后,都会转成SPSS格式或EXCEL格式保存). 辅助包sjmisc进行数据转 ...
- 半主机模式和_MICROLIB 库
半主机是这么一种机制,它使得在ARM目标上跑的代码,如果主机电脑运行了调试器,那么该代码可以使用该主机电脑的输入输出设备. 这点非常重要,因为开发初期,可能开发者根本不知道该 ARM 器件上有什么 ...
- 6. Reverse Linked List 逆转单链表
逆转单链表,比较简单,不细讲,扫描依次改变指针指向. class Solution { public: ListNode* reverseList(ListNode* head) { if(head= ...
- SpringCloud微服务实战——搭建企业级开发框架(三十):整合EasyExcel实现数据表格导入导出功能
批量上传数据导入.数据统计分析导出,已经基本是系统必不可缺的一项功能,这里从性能和易用性方面考虑,集成EasyExcel.EasyExcel是一个基于Java的简单.省内存的读写Excel的开源项 ...
- 如何在Swagger2或Swagger3中增加Json Web Token
1. 前言 Swagger 3.0已经发布有一段时间了,作为一个非常有用的文档工具已经越来越多的项目在使用它.而JWT也是目前前后端分离最常用的安全技术.那么如何在Swagger 3.0 中添加JWT ...
- entfrm-boot开发平台功能介绍【entfrm开源模块化无代码开发平台】
简介 entfrm开发平台,是一个以模块化为核心的无代码开发平台,是一个集PC和APP快速开发.系统管理.运维监控.开发工具.OAuth2授权.可视化数据源管理与数据构建.API动态生成与统计.工作流 ...
- springboot+vue脚手架使用nginx前后端分离
1.vue配置 /** * * 相对于该配置的nginx服务器请参考nginx配置文件 * */ module.exports = { // 基本路径 publicPath: '/', // 输出文件 ...
- redis入门到精通系列(六):redis的事务详解
(一)事务的概念 谈到数据库的高级应用,不可避免会谈到事务.熟悉mysql的朋友们对事务肯定不陌生,简单来讲事务就是控制一个数据库操作序列要么全部执行要么全部不执行.今天我们就来了解redis中的事务 ...
- Spring Boot中使用Redis
一.定义工程 创建一个spring boot模块 二.修改pom文件 在pom文件中添加Spring Boot与Redis整合依赖 <dependencies> <!--spring ...