1041. Robot Bounded In Circle
本题题意:
一开始一个机器人站在了(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的更多相关文章
- 【LeetCode】1041. Robot Bounded In Circle 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 找规律 日期 题目地址:https://leetco ...
- 【leetcode】1041. Robot Bounded In Circle
题目如下: On an infinite plane, a robot initially stands at (0, 0) and faces north. The robot can recei ...
- LeetCode 1041. Robot Bounded In Circle (困于环中的机器人)
题目标签:Math 题目让我们判断机器人是否是一直在走一个圈. 当我们把 instructions 走完一遍时候: 1. 如果机器人回到了原点,那么它是在走一个圈. 2. 如果机器人的方向没有改变,那 ...
- leetcode 1041. 困于环中的机器人
题目地址 : https://leetcode-cn.com/problems/robot-bounded-in-circle/ 在无限的平面上,机器人最初位于 (0, 0) 处,面朝北方.机器人可以 ...
- Leetcode 第136场周赛解题报告
周日的比赛的时候正在外面办事,没有参加.赛后看了下题目,几道题除了表面要考的内容,还是有些能发散扩展的地方. 做题目不是最终目的,通过做题发现知识盲区,去研究学习,才能不断提高. 理论和实际是有关系的 ...
- Judge Route Circle
Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot m ...
- Judge Route Circle --判断圆路线
Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot m ...
- 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 ...
- [LeetCode] Judge Route Circle 判断路线绕圈
Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot m ...
随机推荐
- Emoji 映射编码
Emoji官网:https://emojipedia.org/ Name Unified DoCoMo KDDI Softbank Google Wechat black sun with r ...
- Add a Simple Action using an Attribute 使用特性添加简单按钮
In the previous Add a Simple Action lesson, you learned how to add an Action by implementing the Vie ...
- Add a Class from the Business Class Library从业务类库添加类(EF)
In this lesson, you will learn how to use business classes from the Business Class Library as is. Fo ...
- JavaScript 使用new关键字调用函数
使用new关键字调用函数 test.js 代码如下 function Person(name, age, obj) { var o = new Object(); o.name = name; o.a ...
- Linux-正则、grep、sed学习笔记
一.正则和grep 正则表达式的实现分成了两类: 基本正则表达式(BRE)和扩展的正则表达式(ERE). BRE 和 ERE 之间有什么区别呢?这是关于元字符的问题.BRE 可以辨别以下元字符: ^ ...
- could not launch process: debugserver or lldb-server not found: install XCode's command line tools or lldb-server
0x00 事件 VS 调试 go 的时候,发生了这个错误,导致无法调试: could not launch process: debugserver or lldb-server not found: ...
- Master Note for Transportable Tablespaces (TTS) -- Common Questions and Issues (Doc ID 1166564.1)
APPLIES TO: Oracle Database Cloud Exadata Service - Version N/A and laterOracle Database Cloud Servi ...
- hexdump 工具使用 和 .txt 文件的二进制查看
最近使用txt文件进行数据处理的时候,突然发现txt文件是怎样编码数据的了,它是以二进制来进行存储的吗?为了知道这个情况,我使用hexdump工具进行查看txt文件的二进制形式,并顺道进行学习了hex ...
- Linux-3.14.12内存管理笔记【建立内核页表(3)
前面已经分析了内核页表的准备工作以及内核低端内存页表的建立,接着回到init_mem_mapping()中,低端内存页表建立后紧随着还有一个函数early_ioremap_page_table_ran ...
- JS---DOM---为元素解除绑定事件
解除绑定事件: 1.解绑事件 对象 .on 事件名字=事件处理函数--->绑定事件. 对象 .on 事件名字 = null . 注意:用什么方式绑定事件,就应该用对应的方式解除绑定事件. //1 ...