leetcode 1041——困于环中的机器人
描述:
在无限的平面上,机器人最初位于 (0, 0) 处,面朝北方。机器人可以接受下列三条指令之一:
"G":直走 1 个单位"L":左转 90 度"R":右转 90 度
机器人按顺序执行指令 instructions,并一直重复它们。
只有在平面中存在环使得机器人永远无法离开时,返回 true。否则,返回 false。
示例 1:
输入:"GGLLGG"
输出:true
解释:
机器人从 (0,0) 移动到 (0,2),转 180 度,然后回到 (0,0)。
重复这些指令,机器人将保持在以原点为中心,2 为半径的环中进行移动。
示例 2:
输入:"GG"
输出:false
解释:
机器人无限向北移动。
示例 3:
输入:"GL"
输出:true
解释:
机器人按 (0, 0) -> (0, 1) -> (-1, 1) -> (-1, 0) -> (0, 0) -> ... 进行移动。 思路:循环四次后能回到原点(0,0)的符合
C++:
class Solution {
public:
bool isRobotBounded(string instructions) {
int a=,b=,c=;//(a,b)表示坐标,c表示方向
instructions+=instructions+instructions+instructions;
for(int i=;i<instructions.length();i++){
if(instructions[i]=='G'){
if(c==) b++;
else if(c==) a--;
else if(c==) b--;
else a++;
}else if(instructions[i]=='L'){
if(c==) c=;
else c++;
}else if(instructions[i]=='R'){
if(c==) c=;
else c--;
}
}
return a==&&b==;
}
};
python:
class Solution:
def isRobotBounded(self, instructions: str) -> bool:
a=0
b=0
c=0
instructions+=instructions+instructions+instructions
for i in instructions:
if i=='G':
if c==0: b+=1
elif c==1: a-=1
elif c==2: b-=1
else: a+=1
elif i=='L':
if c==3: c=0
else: c+=1
elif i=='R':
if c==0: c=3
else: c-=1
return a==0 and b==0
leetcode 1041——困于环中的机器人的更多相关文章
- leetcode 1041. 困于环中的机器人
题目地址 : https://leetcode-cn.com/problems/robot-bounded-in-circle/ 在无限的平面上,机器人最初位于 (0, 0) 处,面朝北方.机器人可以 ...
- LeetCode 1041. Robot Bounded In Circle (困于环中的机器人)
题目标签:Math 题目让我们判断机器人是否是一直在走一个圈. 当我们把 instructions 走完一遍时候: 1. 如果机器人回到了原点,那么它是在走一个圈. 2. 如果机器人的方向没有改变,那 ...
- [LeetCode] 874. Walking Robot Simulation 走路机器人仿真
A robot on an infinite grid starts at point (0, 0) and faces north. The robot can receive one of th ...
- [LeetCode] 489. Robot Room Cleaner 扫地机器人
Given a robot cleaner in a room modeled as a grid. Each cell in the grid can be empty or blocked. Th ...
- LeetCode #657. Robot Return to Origin 机器人能否返回原点
https://leetcode-cn.com/problems/robot-return-to-origin/ 设置 flagUD 记录机器人相对于原点在纵向上的最终位置 flagRL 记录机器人相 ...
- Python解答力扣网站题库简单版----第三讲
1041. 困于环中的机器人 题库链接: 1041. 困于环中的机器人. 题干 在无限的平面上,机器人最初位于 (0, 0) 处,面朝北方.机器人可以接受下列三条指令之一: "G" ...
- Leetcode 第136场周赛解题报告
周日的比赛的时候正在外面办事,没有参加.赛后看了下题目,几道题除了表面要考的内容,还是有些能发散扩展的地方. 做题目不是最终目的,通过做题发现知识盲区,去研究学习,才能不断提高. 理论和实际是有关系的 ...
- 剑指offer刷题总结
★ 二维数组的查找 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否 ...
- 6专题总结-动态规划dynamic programming
专题6--动态规划 1.动态规划基础知识 什么情况下可能是动态规划?满足下面三个条件之一:1. Maximum/Minimum -- 最大最小,最长,最短:写程序一般有max/min.2. Yes/N ...
随机推荐
- 四. jenkins部署springboot项目(1)--window环境
前提:jenkins和springboot运行在同一台机器 springboot项目使用git和maven jenkins所需的插件如Maven,Git等这里就不再详述. 1.jenkins配置git ...
- 公司-浪潮:浪潮/inspur
ylbtech-公司-浪潮:浪潮/inspur 浪潮集团有限公司,即浪潮集团,是中国本土综合实力强大的大型IT企业之一,中国领先的云计算.大数据服务商.浪潮集团旗下拥有浪潮信息.浪潮软件.浪潮国际.华 ...
- NtCallbackReturn是否导致了用户态栈的不平衡
0:000> u ntdll!KiFastSystemCall ntdll!KiFastSystemCall: 7c92eb8b 8bd4 mov edx,esp 7c92eb8d 0f34 s ...
- linux 下安装与使用
一 安装 ## 先用wget下载源文件 wget http://download.redis.io/releases/redis-3.2.9.tar.gz ## 我自己建个文件夹 mkdir /usr ...
- 在webpack4 中利用Babel 7取消严格模式方法
报错信息: Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on stri ...
- Visual Studio 2013创建并运行Cocos2d-x工程
| 版权声明:本文为博主原创文章,未经博主允许不得转载. 1.准备: 我们得先把Visual Studio 2013安装好:可以去MSDN官网下载,在安装好 2.安装好vs之后,在cmd(终端)创 ...
- bootstrapTble 的一些小结
前言: 1.bootstrapTable 官网 http://bootstrap-table.wenzhixin.net.cn/zh-cn/ ,http://bootstrap-table.wenz ...
- OOP三大特性及几大设计原则
封装: 1.隐藏实现细节:2.恰当地公开接口:3.将接口和实现分开,增强可维护性:(实现细节改变时,使用该类的客户端程序不需要改变) 继承: 1.描述联结类的层次模型;2.通过抽象,表达共性,实现类的 ...
- Hibernate4教程一:入门介绍
第一部分:Hibernate入门 Hibernate是什么 Hibernate是一个轻量级的ORM框架 ORM原理(Object Relational Mapping) ORM ...
- 【转载】vue install报错run `npm audit fix` to fix them, or `npm audit` for details html
原链接https://www.jianshu.com/p/60591cfc6952 执行npm install 出现如下提醒 added 253 packages from 162 contribut ...