【leetcode】1041. Robot Bounded In Circle
题目如下:
On an infinite plane, a robot initially stands at
(0, 0)
and faces north. The robot can receive one of three instructions:
"G"
: go straight 1 unit;"L"
: turn 90 degrees to the left;"R"
: turn 90 degress to the right.The robot performs the
instructions
given in order, and repeats them forever.Return
true
if and only if there exists a circle in the plane such that the robot never leaves the circle.Example 1:
Input: "GGLLGG"
Output: true
Explanation:
The robot moves from (0,0) to (0,2), turns 180 degrees, and then returns to (0,0).
When repeating these instructions, the robot remains in the circle of radius 2 centered at the origin.Example 2:
Input: "GG"
Output: false
Explanation:
The robot moves north indefinetely.Example 3:
Input: "GL"
Output: true
Explanation:
The robot moves from (0, 0) -> (0, 1) -> (-1, 1) -> (-1, 0) -> (0, 0) -> ...Note:
1 <= instructions.length <= 100
instructions[i]
is in{'G', 'L', 'R'}
解题思路:看到这个题目,我的感觉就是如果能回到起点,应该是执行instructions一次,两次或者四次。嘿嘿,当然我也不知道怎么证明,反正能AC。
代码如下:
class Solution(object):
def process(self,start,instructions):
for i in instructions:
if i == 'G':
if start[2] == 'N':start[1] += 1
elif start[2] == 'S':start[1] -= 1
elif start[2] == 'E':start[0] += 1
elif start[2] == 'W':start[0] -= 1
elif i == 'L':
if start[2] == 'N':start[2] = 'W'
elif start[2] == 'S':start[2] = 'E'
elif start[2] == 'E':start[2] = 'N'
elif start[2] == 'W':start[2] = 'S'
elif i == 'R':
if start[2] == 'N':start[2] = 'E'
elif start[2] == 'S':start[2] = 'W'
elif start[2] == 'E':start[2] = 'S'
elif start[2] == 'W':start[2] = 'N'
return start def isRobotBounded(self, instructions):
"""
:type instructions: str
:rtype: bool
"""
start = [0,0,'N']
end = self.process(start,instructions)
if end[0] == end[1] == 0:
return True
end = self.process(start, instructions)
if end[0] == end[1] == 0:
return True
end = self.process(start, instructions)
end = self.process(start, instructions)
if end[0] == end[1] == 0:
return True
return False
【leetcode】1041. Robot Bounded In Circle的更多相关文章
- 【LeetCode】1041. Robot Bounded In Circle 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 找规律 日期 题目地址:https://leetco ...
- 【leetcode】657. Robot Return to Origin
Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...
- LeetCode 1041. Robot Bounded In Circle (困于环中的机器人)
题目标签:Math 题目让我们判断机器人是否是一直在走一个圈. 当我们把 instructions 走完一遍时候: 1. 如果机器人回到了原点,那么它是在走一个圈. 2. 如果机器人的方向没有改变,那 ...
- 【LeetCode】657. Robot Return to Origin 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 复数求和 counter统计次数 相似题目 参考资料 ...
- 1041. Robot Bounded In Circle
本题题意: 一开始一个机器人站在了(0,0)上,面朝的方向是北,收到三个序列G,L,R. G:直走 L:向左转 R:向右转 按序执行,永远重复. 返回TRUE,如果处在一个圈. 第一个卡住的点: 1. ...
- 【LeetCode】657. Judge Route Circle 解题报告
[LeetCode]657. Judge Route Circle 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/judge-route- ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
随机推荐
- AtomicBoolean 源码分析
AtomicBoolean AtomicBoolean 能解决什么问题?什么时候使用 AtomicBoolean? 可原子更新的 boolean 值 1)原子性:在Java中,对基本数据类型变量的读取 ...
- liunx 定时任务crontab格式说明
每分钟执行 */1 * * * * 每小时执行 0 * * * * 每天执行 0 0 * * * 每周执行 0 0 * * 0 每月执行 0 0 1 * * 每年执行 0 0 1 1 *
- clientdataset 读取excel 如果excel 文件不存在的时候 相应的gird 会不显示数据, 鼠标掠过 gird 格子 才会显示数据。 这是一个bug 哈哈
clientdataset 读取excel 如果excel 文件不存在的时候 相应的gird 会不显示数据, 鼠标掠过 gird 格子 才会显示数据. 这是一个bug 哈哈
- linux下安装php扩展的redis
bu要在网上顺便找个redis扩展,一是不安全,而是,别人的redis可能只能new,能连接,但是不保证是否能进行其他操作, 1.百度一下php redis扩展. 搜索到这个网址:http://pec ...
- [10期]浅谈SSRF安全漏洞
引子:SSRF 服务端请求伪造攻击 很多web应用都提供从其他服务器上获取数据的功能.使用用户指定的URL,web应用可以从其他服务器获取图片,下载文件,读取文件内容等. 这个功能被恶意使用的话,可以 ...
- IDEA常用快揵键
IDEA常用快揵键 工作中常用IDEA快捷键 参见博客:https://www.cnblogs.com/zhangpengshou/p/5366413.html Double shift --- ...
- powerdisigner
- [Git] 003 初识 Git 与 GitHub 之加入文件 第二弹
在 GitHub 的 UI 界面使用 Git 往仓库里加文件 第二弹 1. 选择已有的文件,点击右侧的 edit 2. 在文件中继续写入文字 小发现:我只写到第 6 行,commit 后再点进去,发现 ...
- A*(A_star)搜索总结
\(A^*(A star)\)搜索总结 标签:算法--搜索 阅读体验:https://zybuluo.com/Junlier/note/1299772 定义 先复制一则定义 \(A^*\)算法在人工智 ...
- vue 使用jssdk分享
背景 在vue中使用jssdk微信分享 weixin-js-sdk mint-ui需要安装npm install weixin-js-sdk mint-ui --save mixins/wechat. ...