本题题意:

一开始一个机器人站在了(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. Android给Viewpager默认指定页

    上结果代码 private void setViewPaperItem(int position) { try { Class c = Class.forName("android.supp ...

  2. JAVAEE学期总结

         声明:除第一张思维导图为博主所制作,其他思维导图皆来自网络,若侵权,望告知,必删除.                                                      ...

  3. Java生鲜电商平台-商城系统库存问题分析以及产品设计对逻辑/物理删除思考

    Java生鲜电商平台-商城系统库存问题分析以及产品设计对逻辑/物理删除思考 说明:在生鲜电商的库存设计,是后台的重点,也是难点,关乎商品是否存在超卖.商品的库存增加方式倒不难,直接在后台添加即可,而扣 ...

  4. angularjs用回车键动态添加数据,同时渲染到页面

    <script src="../../angular-1.5.5/angular.min.js"></script> <script> var ...

  5. SQL Server 查询某一个数据库存储过程、函数是否包含某一个内容或者脚本

    SELECT obj.Name 名称, sc.TEXT 内容FROM syscomments scINNER JOIN sysobjects obj ON sc.Id = obj.IDWHERE sc ...

  6. Makefile 文件格式;makefile伪目标

    Makefile包含 目标文件.依赖文件.可运行命令三部分. 每部分的基本格式例如以下: test: prog.o  code.o gcc  -o  test   prog.o   code.o 当中 ...

  7. [日常] windows下使用vscode配合xebug调试php脚本

    windows下使用vscode配合xebug调试php脚本 要下载有php_xebug.dll扩展的版本,最新版可能没有这个扩展,php7.3应该是有的,php7.3.4好像没有默认是不加载这个扩展 ...

  8. Day5- Python基础5 模块导入、time、datetime、random、os、sys、hashlib、json&pickle

    本节目录: 1.模块的分类 2.模块的导入 3.time模块 4.datetime模块 5.random 6.os模块 7.sys模块 8.hashlib 9.json&pickle 一.模块 ...

  9. Eclipse的使用需要注意的问题

    一.修改/设置字符集编码 设置工作空间编码格式 Window--Preference--General--Workspace下,面板''Text file encoding"选择UTF-8格 ...

  10. 修改SQL Server中的计算机名

    安装SQL Server之后,如果修改计算机名会导致登录异常,或者某些功能不能用,例如配置Replication时会提示如下错误: SQL Server replication requires th ...