题目如下:

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. 1 <= instructions.length <= 100
  2. 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的更多相关文章

  1. 【LeetCode】1041. Robot Bounded In Circle 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 找规律 日期 题目地址:https://leetco ...

  2. 【leetcode】657. Robot Return to Origin

    Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...

  3. LeetCode 1041. Robot Bounded In Circle (困于环中的机器人)

    题目标签:Math 题目让我们判断机器人是否是一直在走一个圈. 当我们把 instructions 走完一遍时候: 1. 如果机器人回到了原点,那么它是在走一个圈. 2. 如果机器人的方向没有改变,那 ...

  4. 【LeetCode】657. Robot Return to Origin 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 复数求和 counter统计次数 相似题目 参考资料 ...

  5. 1041. Robot Bounded In Circle

    本题题意: 一开始一个机器人站在了(0,0)上,面朝的方向是北,收到三个序列G,L,R. G:直走 L:向左转 R:向右转 按序执行,永远重复. 返回TRUE,如果处在一个圈. 第一个卡住的点: 1. ...

  6. 【LeetCode】657. Judge Route Circle 解题报告

    [LeetCode]657. Judge Route Circle 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/judge-route- ...

  7. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  8. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  9. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

随机推荐

  1. Linux内核调试方法总结之bugreport

    bugreport [用途]Android性能分析工具,bugreport记录了Android启动过程日志,启动后的系统状态,包括进程列表.内存信息.VM信息等 [使用方法] Adb bugrepor ...

  2. fedora禁用(开机启动)服务和进程管理

    首先要查看有哪些(开机启动)服务 chkconfig --list 或者: systemctl list-units 然后, 根据需要进行禁用服务的开机启动: chkconfig service_na ...

  3. ubuntu 安装maven

    1.下载maven文件 切换目录 root@ubuntu:~# cd /usr/local 下载文件 root@ubuntu:/usr/local# wget http://mirror.bit.ed ...

  4. drop_duplicates()函数

    1dataframe删除某一列的重复元素,默认只留下第一次出现的 inplace参数设置为true时直接在原数据上修改,为False时,生成副本. 注意所有函数中inplace一旦设置为True,此时 ...

  5. JavaScript 变量,语句

    定义变量的方式: var   变量可以没有初始值,变量可以修改,变量可以覆盖,存在变量提升. // 变量提升机制 console.log(name)// undefined var name = &q ...

  6. Winform 开源控件库( Sheng.Winform.Controls)

    升讯威 .Net WinForm 控件库提供了超过15种 Winform 控件,你可以直接使用本控件库,更可以通过本控件库学到 Winform 控件开发的方法和理念. 你可以学习到: 如何基于 Con ...

  7. centos 7 lnmp环境编译安装zabbix-3.4.14

    一.安装环境(zabbix3.0需要php在5.5版本以上) [root@localhost ~]# cat /etc/redhat-release CentOS Linux release (Cor ...

  8. 【ABAP系列】SAP ABAP中使用for all entries in小结

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP中使用for a ...

  9. jdbc步骤:

    一.注册数据库驱动 Class.forName("com.mysql.jdbc.Driver"); 二.建立连接(Connection) Connection conn = Dri ...

  10. Hand on Machine Learning 第二章:端到端的机器学习

    1.import 模块 import os import tarfile from six.moves import urllib import pandas as pd pd.set_option( ...