本题题意:

一开始一个机器人站在了(0,0)上,面朝的方向是北,收到三个序列G,L,R。

G:直走

L:向左转

R:向右转

按序执行,永远重复。

返回TRUE,如果处在一个圈。

第一个卡住的点:

1.疑惑于机器人会不会不经过原点,然后还会出现一个圈?

不会。若在固定路线转圈,肯定是重复了若干次,回到了原点。否则路线是不能固定的。

idea:

1.如果第一次执行完一串指令后,就在原点,则一定是固定路线。

2.如果执行完一串指令后,不在原点,新的方向是dir,则有下面的结论:

dir不是北方,就一定可以回到原点。

证明:

如果dir指向南方,则下一次执行完,就一定回到原点(方向180度)

如果dir指向东方,则四次执行完,就一定回到原点(方向90度)

更简单的讲,机器人重复四次执行,判断是否在原点就可以。

第一次代码:

def isRobotBounded(self, instructions: str) -> bool:
a = instructions* 4
d = 1
"""
1
2 4
3
"""
r = [0,1,2,3,4]
p = [0,0]
for i in a:
if i == 'L':
d = r[d + 1 if d + 1 != 5 else 1]
elif i == 'R':
d = r[d - 1 if d - 1 != 0 else 4]
else:
if d == 1:
p[1] += 1 #y + 1
elif d == 2:
p[0] -= 1 #x - 1
elif d == 3:
p[1] -= 1
else :
p[0] += 1
return p == [0,0]

第二次代码

def isRobotBounded(self, instructions: str) -> bool:
a = instructions* 4
d = 0
"""
0
3 1
2
i = (i + 1) % 4 will turn right
i = (i + 3) % 4 will turn left
"""
rec = [[0,1],[1,0],[0,-1],[-1,0]]
x, y = 0, 0
for i in a:
if i == 'L': d = (d + 3) % 4
elif i == 'R': d = (d + 1) % 4
else: x, y = x + rec[d][0], y + rec[d][1]
return x == 0 and y ==0

总的来说,就是菜。。 照着大神代码看- - - - - -

1041. Robot Bounded In Circle的更多相关文章

  1. 【LeetCode】1041. Robot Bounded In Circle 解题报告(Python)

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

  2. 【leetcode】1041. Robot Bounded In Circle

    题目如下: On an infinite plane, a robot initially stands at (0, 0) and faces north.  The robot can recei ...

  3. LeetCode 1041. Robot Bounded In Circle (困于环中的机器人)

    题目标签:Math 题目让我们判断机器人是否是一直在走一个圈. 当我们把 instructions 走完一遍时候: 1. 如果机器人回到了原点,那么它是在走一个圈. 2. 如果机器人的方向没有改变,那 ...

  4. leetcode 1041. 困于环中的机器人

    题目地址 : https://leetcode-cn.com/problems/robot-bounded-in-circle/ 在无限的平面上,机器人最初位于 (0, 0) 处,面朝北方.机器人可以 ...

  5. Leetcode 第136场周赛解题报告

    周日的比赛的时候正在外面办事,没有参加.赛后看了下题目,几道题除了表面要考的内容,还是有些能发散扩展的地方. 做题目不是最终目的,通过做题发现知识盲区,去研究学习,才能不断提高. 理论和实际是有关系的 ...

  6. Judge Route Circle

    Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot m ...

  7. Judge Route Circle --判断圆路线

    Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot m ...

  8. LeetCode - 657. Judge Route Circle

    Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot m ...

  9. [LeetCode] Judge Route Circle 判断路线绕圈

    Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot m ...

随机推荐

  1. Java并发专题

    ——参考于码农求职小助手公众号 1.并行和并发有什么区别? 1. 并行是指两个或者多个事件在同一时刻发生:而并发是指两个或多个事件在同一时间间隔发生: 2. 并行是在不同实体上的多个事件,并发是在同一 ...

  2. 【转载】作为Android开发者,你真的熟悉Activity吗?

    学过android的人都知道,activity是最常用的四大组件之一,但你真的了解透彻activity了吗?接下来,本人将从activity的正常和异常生命周期.启动模式.IntentFilter匹配 ...

  3. [logstash.outputs.elasticsearch] retrying failed action with response code: 403

    0x00 Event [2019-09-24T19:22:31,655][INFO ][logstash.outputs.elasticsearch] retrying failed action w ...

  4. linux学习(五)用户与组管理命令,以及用户信息文件解释

    目录 (1)/etc/passwd文件 (2)/etc/shadow passwd命令 userdel命令 usermod命令 groupadd @(用户与组管理命令) linux是一个多用户多任务的 ...

  5. django models 数据库操作

    django models 数据库操作 创建模型 实例代码如下 from django.db import models class School(models.Model): pass class ...

  6. 快速破解Goland

    两种激活方式永久激活:推荐优先使用,永久有效有效期激活:如果你实在激活不了又着急使用,这是备选激活方案,简单快捷 一.永久激活 1.下载新版破解补丁 点击链接 https://pan.baidu.co ...

  7. 图片base64编码解码

    1.图片base64编码 https://c.runoob.com/front-end/59 2.图片base64解码 https://www.it399.com/image/base64 https ...

  8. 从傅里叶变换(FFT)到数论变换(NTT)

    FFT可以用来计算多项式乘法,但是复数的运算中含有大量的浮点数,精度较低.对于只有整数参与运算的多项式,有时,\(\text{NTT(Number-Theoretic Transform)}\)会是更 ...

  9. Codeforces Round #598 (Div. 3) B. Minimize the Permutation 贪心

    B. Minimize the Permutation You are given a permutation of length n. Recall that the permutation is ...

  10. 通过yum在centos安装mysql并配置远程登录

    前言 前天按照Oracle上的文档装了一遍mysql,选了最新8.0的版本,后来出现一些问题,网上搜答案,出来的基本还是5.x版本的解决方案,并不适用8.0版本.然后我就去看了一下公司的正式环境买的阿 ...