LeetCode 1041. Robot Bounded In Circle (困于环中的机器人)
题目标签:Math
题目让我们判断机器人是否是一直在走一个圈。
当我们把 instructions 走完一遍时候:
1. 如果机器人回到了原点,那么它是在走一个圈。
2. 如果机器人的方向没有改变,那么它一直在朝一个方向走,就算重复instructions也不可能会走一个圈。
所以当机器人的方向改变的话,在重复instructions,它一定会走回原点,完成一个圈。
具体看code。
Java Solution:
Runtime: 0 ms, faster than 100 %
Memory Usage: 34 MB, less than 100 %
完成日期:07/31/2019
关键点:根据机器人方向改变来判断
class Solution {
public boolean isRobotBounded(String instructions) {
int x = 0, y = 0; // x, y location
int i = 0; // robot's direction
int d[][] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; // 4 directions
for(int j = 0; j < instructions.length(); j++)
{
if(instructions.charAt(j) == 'R')
i = (i + 1) % 4;
else if(instructions.charAt(j) == 'L')
i = (i + 3) % 4;
else { // G: update x, y
x += d[i][0];
y += d[i][1];
}
}
return (x == 0 && y == 0) || i > 0; // back to origin or facing not north
}
}
参考资料:LeetCode Discuss
LeetCode 题目列表 - LeetCode Questions List
题目来源:https://leetcode.com/
LeetCode 1041. Robot Bounded In Circle (困于环中的机器人)的更多相关文章
- 【LeetCode】1041. Robot Bounded In Circle 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 找规律 日期 题目地址:https://leetco ...
- 【leetcode】1041. Robot Bounded In Circle
题目如下: On an infinite plane, a robot initially stands at (0, 0) and faces north. The robot can recei ...
- 1041. Robot Bounded In Circle
本题题意: 一开始一个机器人站在了(0,0)上,面朝的方向是北,收到三个序列G,L,R. G:直走 L:向左转 R:向右转 按序执行,永远重复. 返回TRUE,如果处在一个圈. 第一个卡住的点: 1. ...
- leetcode 1041. 困于环中的机器人
题目地址 : https://leetcode-cn.com/problems/robot-bounded-in-circle/ 在无限的平面上,机器人最初位于 (0, 0) 处,面朝北方.机器人可以 ...
- leetcode 1041——困于环中的机器人
描述: 在无限的平面上,机器人最初位于 (0, 0) 处,面朝北方.机器人可以接受下列三条指令之一: "G":直走 1 个单位 "L":左转 90 度 &quo ...
- 【LeetCode】657. Judge Route Circle 解题报告
[LeetCode]657. Judge Route Circle 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/judge-route- ...
- LeetCode 489. Robot Room Cleaner
原题链接在这里:https://leetcode.com/problems/robot-room-cleaner/ 题目: Given a robot cleaner in a room modele ...
- [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
There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of its mov ...
随机推荐
- sql 链接查询
连接查询是另一种类型的多表查询.连接查询对多个表进行JOIN运算,简单地说,就是先确定一个主表作为结果集,然后,把其他表的行有选择性地“连接”在主表结果集上. 例如,我们想要选出students表的所 ...
- (转)Openfire 中SASL的认证方式之:PLAIN,DIGEST-MD5,anonymous
转:http://blog.csdn.net/coding_me/article/details/39524137 SASL 的认证方式包括: 1. PLAIN:plain是最简单的机制,但 ...
- int在64位操作系统中占多少位?
仍然是32位. 曾经是这样的:16位操作系统中,int 占16位:在32位操作系统中,int 占32位.但是现在人们已经习惯了 int 占32位,因此在64位操作系统中,int 仍为32位.64位整型 ...
- 树莓派自动播报温湿度到QQ空间、微博
原文链接 https://aoaoao.me/951.html 这是个比较无聊的应用...灵感来自于一个叫做“古城钟楼”的微博账号,此账号每天都会定点报时,除此之外没有其他任何内容,以此吸引了近50万 ...
- js 为什么计算结果老是出现NaN
js 为什么计算结果老是出现NaN 可能原因: 1.操作的两个数,类型不一致 2.有一个值为NaN,计算後为NaN 1. 转换函数: js提供了parseInt()和parseFloat()两个转换函 ...
- 23. Jmeter使用ServerAgent对服务器进行性能监控
我们在做服务器性能测试的时候,往往会考虑四个点:CPU.网络.磁盘.内存.一般情况下是使用Linux命令进行监控,那么jmeter可否做到呢?答案是可以的,闲话不多说,进入正题. 环境准备 jmete ...
- normal use for autotools
1. remove temporary files, only used for test purpose. ls | sed -e rm -rf 2. edit autogen.sh echo &q ...
- PDO如何完成事务操作
起因 无意间翻看极客学院的APP,准备找一些教程看看.看到一篇PDO 安全处理与事务处理,一想对MySQL的事务处理仅仅停留在概念上(知道执行多条语句,其中一个失败了,就会回滚操作).但是把概念变成代 ...
- boost asio tcp 多线程异步读写,服务器与客户端。
// server.cpp #if 0 多个线程对同一个io_service 对象处理 用到第三方库:log4cplus, google::protobuf 用到C++11的特性,Windows 需要 ...
- svg圆环缓冲动画
代码如下 <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8& ...