作者: 负雪明烛
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 日 —— 浑浑噩噩的一天

最新文章

  1. Silverlight和WPF中DataContractJsonSerializer对时间的处理差异
  2. 《Entity Framework 6 Recipes》翻译系列(2) -----第一章 开始使用实体框架之使用介绍
  3. html5的spellcheck属性(拼写、文法检查)
  4. Step by step Process of creating APD
  5. 二元查找树转变成排序的双向链表之C#算法实现
  6. android内存泄露调试,Heap,MAT
  7. JS调用android逻辑方法
  8. linux svn服务器普通passwd和sasl2配置
  9. 【Linux】环境变量设置
  10. 【实验手册】使用Visual Studio Code 开发.NET Core应用程序
  11. Rlwrap工具的安装和配置
  12. 基于Webpack, KnockoutJs,esyui,koeasyui实现类vue-cli生成的模板框架
  13. linux 保存git的账号密码
  14. SQL Server利用XML找字符串相同部分
  15. Shiro学习笔记六(自定义Reaml-使用数据库设置 user roles permissions)
  16. db2创建数据库
  17. SQL之group by
  18. css属性所对应js属性
  19. ServiceStack支持跨域提交
  20. s11 day104 数据库表结构与立即支付流程

热门文章

  1. ss 显示socket状态
  2. exit(0) exit(1) return() 3个的区别
  3. 使用SpringBoot实现登录注册的几个问题
  4. C语言中的除法的计算
  5. 学习java 7.18
  6. 26. Linux GIT
  7. Spark基础:(六)Spark SQL
  8. oracle中注释都是问号?中文显示不出来问题
  9. RAC(Reactive Cocoa)常见的类
  10. 【C/C++】最长无重复子数组