本题题意:

一开始一个机器人站在了(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. 在Asp.Net Core中配置使用MarkDown富文本编辑器实现图片上传和截图上传(开源代码.net core3.0)

    我们的富文本编辑器不能没有图片上传尤其是截图上传,下面我来教大家怎么实现MarkDown富文本编辑器截图上传和图片上传. 1.配置编辑器到html页 <div id="test-edi ...

  2. requests-html库render的使用

    一.render的使用 from requests_html import HTMLSession session =HTMLSession() response = session.get('htt ...

  3. SpringCloud(九):springcloud——链路追踪springcloud-sleuth

    Spring-Cloud-Sleuth是Spring Cloud的组成部分之一,为SpringCloud应用实现了一种分布式追踪解决方案,其兼容了Zipkin, HTrace和log-based追踪, ...

  4. JQuery iframe宽高度自适应浏览器窗口大小的解决方法

    iframe宽高度自适应浏览器窗口大小的解决方法   by:授客 QQ:1033553122 1.   测试环境 JQuery-3.2.1.min.js 下载地址: https://gitee.com ...

  5. Python 函數 Function

    函數最初被設計出來,是用來減輕重複 coding 一段相同的代碼,這之間只有代碼 (方法,Method) 的重用,但還沒有物件導向OO整個Object 的屬性與方法被封裝重用的概念. 函數的定義很簡單 ...

  6. 矩阵的运算:Python语言实现

    一.矩阵的加减法 import numpy as np #这里是矩阵的加法 ar1=np.arange(10).reshape(10,1) ar1 ar2=np.arange(10).reshape( ...

  7. Android Service 启动流程

    执行顺序 : startService -> bindService -> unbindService -> stopService 回调的结果为: 执行顺序 : startServ ...

  8. 未能找到元数据文件**.dll解决办法

    解决方案里有很多项目.生成时提示100多个错误,都是未能找到元数据文件**.dll. 那就清理一下解决方案,一个一个来吧. 生成GateWay.Utilities项目时,虽然提示成功了,却发现bin/ ...

  9. Django_xadmin_TypeError: Related Field got invalid lookup: icontains

    问题: 当我在给某一张表加上外键搜索的时候,会出现 TypeError: Related Field got invalid lookup: icontains 问题原因: a 表关联 b表,也就是说 ...

  10. 【转载】Java中的多线程超详细的总结

    引 如果对什么是线程.什么是进程仍存有疑惑,请先Google之,因为这两个概念不在本文的范围之内. 用多线程只有一个目的,那就是更好的利用cpu的资源,因为所有的多线程代码都可以用单线程来实现.说这个 ...