本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。

为了方便在PC上运行调试、分享代码文件,我还建立了相关的仓库。在这一仓库中,你不仅可以看到LeetCode原题链接、题解代码、题解文章链接、同类题目归纳、通用解法总结等,还可以看到原题出现频率和相关企业等重要信息。如果有其他优选题解,还可以一同分享给他人。

由于本系列文章的内容随时可能发生更新变动,欢迎关注和收藏征服LeetCode系列文章目录一文以作备忘。

相关公司:Amazon、LinkedIn、Goldman Sachs

On an infinite plane, a robot initially stands at (0, 0) and faces north. Note that:

  • The north direction is the positive direction of the y-axis.
  • The south direction is the negative direction of the y-axis.
  • The east direction is the positive direction of the x-axis.
  • The west direction is the negative direction of the x-axis.

The robot can receive one of three instructions:

  • "G": go straight 1 unit.
  • "L": turn 90 degrees to the left (i.e., anti-clockwise direction).
  • "R": turn 90 degrees to the right (i.e., clockwise direction).

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: instructions = "GGLLGG"
Output: true
Explanation: The robot is initially at (0, 0) facing the north direction.
"G": move one step. Position: (0, 1). Direction: North.
"G": move one step. Position: (0, 2). Direction: North.
"L": turn 90 degrees anti-clockwise. Position: (0, 2). Direction: West.
"L": turn 90 degrees anti-clockwise. Position: (0, 2). Direction: South.
"G": move one step. Position: (0, 1). Direction: South.
"G": move one step. Position: (0, 0). Direction: South.
Repeating the instructions, the robot goes into the cycle: (0, 0) --> (0, 1) --> (0, 2) --> (0, 1) --> (0, 0).
Based on that, we return true.

Example 2:

Input: instructions = "GG"
Output: false
Explanation: The robot is initially at (0, 0) facing the north direction.
"G": move one step. Position: (0, 1). Direction: North.
"G": move one step. Position: (0, 2). Direction: North.
Repeating the instructions, keeps advancing in the north direction and does not go into cycles.
Based on that, we return false.

Example 3:

Input: instructions = "GL"
Output: true
Explanation: The robot is initially at (0, 0) facing the north direction.
"G": move one step. Position: (0, 1). Direction: North.
"L": turn 90 degrees anti-clockwise. Position: (0, 1). Direction: West.
"G": move one step. Position: (-1, 1). Direction: West.
"L": turn 90 degrees anti-clockwise. Position: (-1, 1). Direction: South.
"G": move one step. Position: (-1, 0). Direction: South.
"L": turn 90 degrees anti-clockwise. Position: (-1, 0). Direction: East.
"G": move one step. Position: (0, 0). Direction: East.
"L": turn 90 degrees anti-clockwise. Position: (0, 0). Direction: North.
Repeating the instructions, the robot goes into the cycle: (0, 0) --> (0, 1) --> (-1, 1) --> (-1, 0) --> (0, 0).
Based on that, we return true.

Constraints:

  • 1 <= instructions.length <= 100
  • instructions[i] is 'G', 'L' or, 'R'.

题意:机器人在一个无限的二维平面上,给它一串移动指令,机器人反复执行这条指令,如果它在平面上重复走一个死循环,就返回 true


解法 模拟

当机器人执行完指令 instructions 后,它的位置和方向均有可能发生变化。我们先考虑位置,然后考虑其方向。

  • 如果它的位置仍位于原点,那么不管它此时方向是什么,都将永远无法离开。
  • 如果它的位置不在原点,我们分情况讨论此时机器人的方向:
    • 如果仍然朝北,那么机器人不会陷入循环。假设执行完一串指令后,机器人的位置是 ( x , y ) (x,y) (x,y) 且不为原点,方向仍然朝北,那么执行完第二串指令后,机器人的位置便成为 ( 2 × x , 2 × y ) (2\times x, 2\times y) (2×x,2×y) ,会不停地往外部移动,不会陷入循环。
    • 如果朝南,那么执行第二串指令时,机器人的位移与第一次相反,即第二次的位移是 ( − x , − y ) (−x,−y) (−x,−y) ,并且结束后会回到原来的方向。这样一来,每两串指令之后,机器人都会回到原点,并且方向朝北,机器人会陷入循环。
    • 如果朝东,即右转了 90 ° 90\degree 90° 。这样一来,每执行一串指令,机器人都会右转 90 ° 90° 90° 。那么第一次和第三次指令的方向是相反的,第二次和第四次指令的方向是相反的,位移之和也为 0 0 0 ,这样一来,每四串指令之后,机器人都会回到原点,并且方向朝北,机器人会陷入循环。如果机器人朝西,也是一样的结果。

因此,机器人想要摆脱循环,在一串指令之后的状态,必须是不位于原点且方向朝北

class Solution {public:bool isRobotBounded(string instructions) {int move[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};int x = 0, y = 0, dir = 0; // 0北,1东,2南,3西for (char &c : instructions) {if (c == 'G') x += move[dir][0], y += move[dir][1];else if (c == 'L') dir = (dir + 3) % 4;else dir = (dir + 1) % 4;}return (x == 0 && y == 0) || dir != 0;}
};

复杂度分析

  • 时间复杂度: O ( n ) O(n) O(n) ,其中 n n n 是 instructions 的长度,需要遍历 instructions 一次。
  • 空间复杂度: O ( 1 ) O(1) O(1) ,只用到常数空间。

LeetCode 1041. Robot Bounded In Circle【字符串,模拟】中等相关推荐

  1. 1041. Robot Bounded In Circle

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

  2. CF5A Chat Server's Outgoing Traffic(字符串模拟,find函数的应用)难度⭐

    题意翻译 Polycarp正在开发一个名为"Polychat"的新项目.按照IT的现代倾向,他决定,这个项目也应该包含聊天.为了实现这一目标,Polycarp在笔记本电脑前花费了几 ...

  3. 剑指offer:汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果。对于一个给定的字符序列S,请你把其循环左移K位后的序列输出。

    汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果.对于一个给定的字符序列S,请你把其循环左移K位后的序列输出.例如,字符序列S="abc ...

  4. 【LeetCode】【HOT】394. 字符串解码(栈)

    [LeetCode][HOT]394. 字符串解码 文章目录 [LeetCode][HOT]394. 字符串解码 package hot;import java.util.LinkedList;pub ...

  5. 【LeetCode】剑指 Offer 38. 字符串的排列

    [LeetCode]剑指 Offer 38. 字符串的排列 文章目录 [LeetCode]剑指 Offer 38. 字符串的排列 package offer;import java.util.Hash ...

  6. 【Luogu1580】yyy loves Easter_Egg I(纯字符串模拟)

    problem 保持队形(我们认为只要这一句内含有且恰好含有一次@,@的人和上一句话一样就算为队形) 若艾特的人与第一个人不同,就算队形被打破,油炸失败.若这个人在队形被打破之前出来吱声了,或者就是他 ...

  7. 【LeetCode】606.根据二叉树创建字符串

    链接: [LeetCode]606.根据二叉树创建字符串 思路 使用二叉树的中序遍历,需要注意的是当左节点为空,右节点不为空的时候要加上(). 代码 class Solution { public:s ...

  8. POJ 3095 Linear Pachinko 字符串模拟

    http://poj.org/problem?id=3095 简单字符串模拟 Linear Pachinko Time Limit:1000MSMemory Limit:65536K Descript ...

  9. LeetCode第1371题:每个元音包含偶数次的最长子字符串(中等)

    LeetCode第1371题:每个元音包含偶数次的最长子字符串(中等) 题目:给你一个字符串 s ,请你返回满足以下条件的最长子字符串的长度:每个元音字母,即 'a','e','i','o','u' ...

最新文章

  1. 15Proxy(代理)模式
  2. 双机热备+Win2003下集群案例
  3. linux手动注入网络数据_Linux网络 - 数据包的接收过程
  4. Django创建项目后,项目文件夹下的组成部分
  5. Java入门, 线程
  6. 扫描器scanner的源代码
  7. linux中fstab文件详解
  8. linux命令cat过滤注释行和空白行
  9. WebDAV被启用(转)
  10. 自然语言处理--中文文本向量化counterVectorizer()
  11. 软考资料-软件设计师
  12. YCOJ中国邮递员问题
  13. 李广难封–有感于团队建设
  14. 《仿美团SSM版》项目研发总结
  15. 空城过客XP系统快捷方式去除箭头方法
  16. Java8 新特性 (五)Stream API
  17. 查SCI索引号和EI索引号的方法
  18. 汇智创科机器人,汇智创科机器人加盟
  19. Shader混合模式--正片叠底、滤色、叠加
  20. python——Matplotlib饼图、直方图的绘制

热门文章

  1. 千元“性能小霸王”国产旗舰手机,性价比超高,手机炸开花!
  2. Graphviz的使用指南
  3. 二、AEM中自定义模板和页面
  4. 在 Java 里找对象需要见家长考核吗?
  5. 织梦mysql怎么修复数据库表_织梦后台如何恢复数据库 织梦数据库文件在哪
  6. vivado报错信息学习过程更新
  7. linux 【监控io】iotop命令详解
  8. 4.2 路由器工作原理
  9. js函数参数不匹配问题
  10. 微型计算机智能体重评测,华为智能体脂秤WiFi版评测:17项身体指标秒知道 与肥胖生活断舍离...