【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 ...
随机推荐
- 数位dp好题整理+自己wa过的细节记录
花(fa)神的数论题 三倍经验:烦人的数学作业 windy数 手机号码 同类分布(博客先鸽着) 板子固然好,细节无限多. 花式wa题法,警示后来人. 1.手残害人不浅 (蒟蒻的我掉坑里不止一次) 2. ...
- fedora23解决gedit和vim中文乱码的问题
fedora23解决gedit和vim中文乱码的问题 a, an, the这些不定/定 冠词并不是在所有的名词 前面都要加. 只有在语义上需要时,才加. 名词的单数/复数 前面不加 冠词的 例子多的是 ...
- CentOS5.5配置Oracle监听 netca
在使用netca 配置监听时总是出现这个错误,即使更改了端口也会报错,,,也是在各种百度下, 找到了一个行之有效的办法: 在root下 step 1:netstat -a | grep 1521 确定 ...
- 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_05 IO字符流_1_字符输入流_Reader类&FileRead
- 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_08 Map集合_6_Map集合遍历键值对方式
增强for
- dockerFile 配置puppeteer
## install npm && puppeteer## 必要依赖 libXScrnSaver RUN yum -y install libXScrnSaver ## install ...
- Whatever happens tomorrow, we've had today
bathe: v. 用水清洗 resemblance:n. 相似.相像 cavity:n. 洞,孔,腔 stubborn: adj. 顽强的,固执的 stillness: n. 静止,沉静 tenth ...
- 数组的includes方法
Array.prototype.includes方法返回一个布尔值,表示某个数组是否包含给定的值,与字符串的includes方法类似.该方法属于 ES7 ,但 Babel 转码器已经支持. [1, 2 ...
- 20191103 《Spring5高级编程》笔记-第3章
第3章 在Spring中引入IoC和DI 依赖注入是IOC的一种特殊形式,尽管这两个术语经常可以互换使用. 3.1 控制反转和依赖注入 IOC的核心是DI,旨在提供一种更简单的机制来设置组件依赖项,并 ...
- oracle--优化思路