题目如下:

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] += 1elif start[2] == 'S':start[1] -= 1elif start[2] == 'E':start[0] += 1elif start[2] == 'W':start[0] -= 1elif 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 startdef isRobotBounded(self, instructions):""":type instructions: str:rtype: bool"""start = [0,0,'N']end = self.process(start,instructions)if end[0] == end[1] == 0:return Trueend = self.process(start, instructions)if end[0] == end[1] == 0:return Trueend = self.process(start, instructions)end = self.process(start, instructions)if end[0] == end[1] == 0:return Truereturn False

转载于:https://www.cnblogs.com/seyjs/p/10853771.html

【leetcode】1041. Robot Bounded In Circle相关推荐

  1. LeetCode 1041. Robot Bounded In Circle【字符串,模拟】中等

    本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12.由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止:由于LeetCode还在不断地创建新 ...

  2. 1041. Robot Bounded In Circle

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

  3. 【Leetcode】100. 相同的树

    题目 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1/ \ / \2 3 2 3[1,2,3], [1 ...

  4. 【leetcode】85. Maximal Rectangle 0/1矩阵的最大全1子矩阵

    1. 题目 Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1 ...

  5. 【leetcode】486. Predict the Winner

    题目如下: Given an array of scores that are non-negative integers. Player 1 picks one of the numbers fro ...

  6. 【leetcode】132. Palindrome Partitioning II

    题目如下: 解题思路:本题是[leetcode]131. Palindrome Partitioning的升级版,要求的是求出最小cuts,如果用[leetcode]131. Palindrome P ...

  7. 【leetcode】86. Partition List

    题目如下: Given a linked list and a value x, partition it such that all nodes less than x come before no ...

  8. 【Leetcode】103. 二叉树的锯齿形层次遍历

    题目 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). 例如: 给定二叉树 [3,9,20,null,null,15,7], 3 ...

  9. 【Leetcode】79.单词搜索

    题目 给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻"单元格是那些水平相邻或垂直相邻的单元格.同一个单元格 ...

  10. 【leetcode】 算法题1 两数之和

    [leetcode] 算法题1 两数之和 问题   给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums ...

最新文章

  1. 生成验证码点击可刷新
  2. SimulatorXcode模拟器如何使用PC键盘进行输入
  3. 第 10 章 建造者模式【Builder Pattern】
  4. php yii框架连接数据库,【PHP开发框架】yii框架怎样衔接数据库
  5. 函数,参数数组params与数组参数,结构函数
  6. CS领域论文数据分析
  7. [示例代码]植物大战僵尸网页版
  8. C# 串口编程,扫码枪使用
  9. TrueLicense 使用JDK自带的 keytool 工具生成公私钥证书库
  10. 无穷级数求和7个公式_Excel技能:职场必备的三个SUM函数、10个求和公式
  11. 学计算机办公软件多久能学好,学好办公软件的方法 如何快速学好办公软件
  12. 计算机屏幕方向变化,知识:如何切换笔记本电脑屏幕的方向_计算机的基本知识_IT /计算机_信息...
  13. 谈谈反向代理Nginx
  14. 唐巧的iOS技术博客选摘
  15. 《番茄todo》APP广告的设计与制作
  16. 如何查看宇视科技摄相机是否支持反向供电
  17. 新华三:助力IPv6部署,我们责无旁贷
  18. 基带传输与频带传输(关系与区别)
  19. 大学四年努力学好编程
  20. 计算机word教案设计,Word文档教学设计

热门文章

  1. 【图像隐写】基于matlab WOW算法图像自适应隐写【含Matlab源码 368期】
  2. spring的九大组件
  3. json字符串和字典的区别
  4. k2p 刷breed_斐讯路由器系列「K1-K2-K2P-K2T」-Breed刷入工具v1.1支持XP系统
  5. opengl画三维点_[500行代码学懂OpenGL]之一画点
  6. 程序员又双叒叕开始装逼了,这次用代码写合租广告,网友神评亮了
  7. 通过vue-cli3构建一个SSR应用程序
  8. HDU 6098 Inversion 思维
  9. 揭秘 IFTTT 每天处理几十亿事件数据的基础结构
  10. c++ 中引用()的用法和应用实例