【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
instructionsgiven in order, and repeats them forever.Return
trueif 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 <= 100instructions[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 ...
随机推荐
- Linux中退出循环命令
[root@a ~]#cat break.sh #!/bin/bash while : #其中“:”表示while循环的条件永远为真的意思 do read -p "Enter a numbe ...
- 局域网IP耗尽
w 大量的vm 大量的实例 动态修改 mac
- (转)用C#实现实现简单的 Ping 的功能,用于测试网络是否已经连通
本文转载自:http://blog.csdn.net/xiamin/archive/2009/02/14/3889696.aspx 用C#实现实现简单的 Ping 的功能,用于测试网络是否已经联通 1 ...
- 在Linux环境中运行python 项目
1首先创建一个虚拟环境或者在一个已有的虚拟环境中创建一个django项目 1.1 创建一个虚拟环境: mkvirtualenv my_django115 这会在 ~/Envs 中创建 my_djang ...
- 三十五、robotframework中怎么将100转化成100.00
1.将100转化成100.00
- RAM: Residual Attention Module for Single Image Super-Resolution
1. 摘要 注意力机制是深度神经网络的一个设计趋势,其在各种计算机视觉任务中都表现突出.但是,应用到图像超分辨领域的注意力模型大都没有考虑超分辨和其它高层计算机视觉问题的天然不同. 作者提出了一个新的 ...
- JS text节点无innerHTML
在线预览 text节点无innerHTML这个属性!!! 如果直接修改text节点的属性(data,nodeValue,textContent),或者使用js原生的修改text节点的内容的方法都会将H ...
- 阶段1 语言基础+高级_1-3-Java语言高级_1-常用API_1_第7节 Arrays工具类_16_数组工具类Array
在java.util的包下面.在这个包的下面是需要导包的,只有lang 的包下面是不需要导包的 查看jdk1.6的手册 Arrays让我们想起了数组,说明它提供了与数组相关的方法 我们可以看到 toS ...
- 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_01 Collection集合_3_Collection集合常用功能
Collection在java.util包下面 只学里面几个比较重要的,List和Set 一共7个共性方法 接口指向实现类,多态的形式. 输出这个结合打印出一个空的数组.说明它重写了toString的 ...
- Week 9 - 638.Shopping Offers - Medium
638.Shopping Offers - Medium In LeetCode Store, there are some kinds of items to sell. Each item has ...