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 ...
随机推荐
- (转)简单移动平均线(Simple Moving Average,SMA) 定义及使用
原文链接:https://blog.csdn.net/Enjolras_fuu/article/details/88602309 扩展:https://www.investopedia.com/t ...
- Java从入门到精通系统书籍,吐血整理的,只要1元
思诚为修身之本,而明善又为思诚之本 我会在支付宝联系你 付完款后在支付宝账单详情页 点击联系收款方.我会回复您下载链接.
- nmap的简单使用
主机探测 1.扫描单个主机 nmap 192.168.1.2 2.扫描整个子网 nmap 192.168.1.1/24 3.扫描多个目标 nmap 192.168.1.1 192.168.1.1.5 ...
- OpenCV:获取图像当中某一点的坐标
import numpy as np image=np.zeros((300,300,3),dtype='uint8') (cx,cy)=image.shape[1]//2,image.shape[0 ...
- Spring Boot 2 使用自定义配置
在application.yml定义配置后,可以使用Environment来读取配置,也可以使用@Value注解让业务代码去读取配置.如果属性较多,可以定义属性映射对象. 开发环境:IntelliJ ...
- Django_xadmin_TypeError: Related Field got invalid lookup: icontains
问题: 当我在给某一张表加上外键搜索的时候,会出现 TypeError: Related Field got invalid lookup: icontains 问题原因: a 表关联 b表,也就是说 ...
- Linux:DHCP服务器的搭建
了解DHCP协议工作原理 DHCP(Dynamic Host Configuration Protocol,动态主机配置协议)提供了动态配置IP地址的功能.在DHCP网络中,客户端不再需要自行输入网络 ...
- 小型数据库性能对比(对比了SQLite、FireBird、FastDB、SQLServer2000绿色版、Access、BDB、PostgreSQL)
一.全面对比 SQLite.FireBird.FastDB.SQLServer2000绿色版.Access.BDB.PostgreSQL对比结果如下: 二.国产数据库 三.SQLite 与Postgr ...
- 服务器jvm参数配置
Eclipse崩溃,错误提示: MyEclipse has detected that less than 5% of the 64MB of Perm Gen (Non-heap memory) s ...
- mongodb简单监控
一.mongodb提供了自己的监控工具 mongostat mongostat是mongodb自带的状态检测工具,在命令行下使用.它会间隔固定时间获取mongodb的当前运行状态,并输出.如果你发现数 ...