作者: 负雪明烛
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)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. LeetCode #657. Robot Return to Origin 机器人能否返回原点

    https://leetcode-cn.com/problems/robot-return-to-origin/ 设置 flagUD 记录机器人相对于原点在纵向上的最终位置 flagRL 记录机器人相 ...

  5. 【leetcode】657. Robot Return to Origin

    Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...

  6. 【Leetcode_easy】657. Robot Return to Origin

    problem 657. Robot Return to Origin 题意: solution1: class Solution { public: bool judgeCircle(string ...

  7. 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 [LeetCode] 题目地址:h ...

  8. 657. Robot Return to Origin

    Description There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequenc ...

  9. 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

随机推荐

  1. R语言与医学统计图形-【12】ggplot2几何对象之条图

    ggplot2绘图系统--几何对象之条图(包括误差条图) 1.条图 格式: geom_bar(mapping = , data = , stat = 'count', #统计变换默认计数 positi ...

  2. 金蝶EAS——客户端打开时,提示正在更新的文件d:\eas\client\bin\lib\proxy.jar被其他应用程序占用.请关闭

    解决办法: 一.通过调用任务管理器来退出,启用任务管理器需同时按下键Ctrl+Alt+Del,在应用程序中找到金蝶EAS,单击,选择结束任务即可:或者在任务管理器中选择"进程",点 ...

  3. 44-Count and Say

    Count and Say My Submissions QuestionEditorial Solution Total Accepted: 79863 Total Submissions: 275 ...

  4. fastJson序列化

    在pojo实体中有map<String,Object>的属性,有个key是user它存储在数据库中是用户的id数组,而在aop里会对这个属性做用户详细信息查询并重新put给user.在做J ...

  5. mango后台

     环境搭建 项目配置 下载后导入项目,删除mvnw.mvnw.cmd两个文件 修改spring-boot-starter-web pom.xml --> run as --> mave i ...

  6. oracle 执行计划的获取方法

    1.用explain plan for来获取执行计划 explain plan for <sql>; select * from table(dbms_xplan.display()); ...

  7. Linux学习 - IP地址配置

    1 首先选择桥接模式 2 配置IP.子网掩码.网关.DNS setup 本例中使用的是无线网连接, IP地址:  192.168.3.195 子网掩码:  255.255.255.0 网关: 192. ...

  8. CentOS6+nginx+uwsgi+mysql+django1.6.6+python2.6.6

    1.配置网关 #vi /etc/sysconfig/network NETWORKING=yes(表示系统是否使用网络,一般设置为yes.如果设为no,则不能使用网络,而且很多系统服务程序将无法启动) ...

  9. 【Service】【Database】【Cache】Redis

    1. 简介: 1.1. redis == REmote DIctionary Server 1.2. KV cache and store, in-memory, 持久化,主从(sentinel实现一 ...

  10. 05 - Vue3 UI Framework - Button 组件

    官网基本做好了,接下来开始做核心组件 返回阅读列表点击 这里 目录准备 在项目 src 目录下创建 lib 文件夹,用来存放所有的核心组件吧.然后再在 lib 文件夹下创建 Button.vue 文件 ...