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 ...
随机推荐
- Linux常用查看日志命令tail
常用查看日志操作语句: tail web.2016-06-06.log -n 300 -f 查看底部即最新300条日志记录,并实时刷新 grep 'ni ...
- java在acm中常用基础技巧方法
java在acm中常用基础技巧方法 如果学到了新的技巧,本博客会更新~ input input-std @Frosero import java.util.*; public class Main { ...
- 框架-.NET:Spring.Net
ylbtech-框架-Spring.Net:Spring.Net Spring.NET为建立企业级应用提供了一套轻量级的解决方案.通过Spring.NET,我们可以用统一且透明的方式来配置应用程序.S ...
- 文件下载java代码
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletExcepti ...
- node.js是用来做什么的
Node.js 使用了一个事件驱动.非阻塞式 I/O 的模型,使其轻量又高效.(事件驱动:事件触发过程中,进行决策的一种策略,简单说就是跟随当前时间点上出现的事物,调用可用的资源进行解决该事物,使得不 ...
- C# - 怎么截取字符串中指定字符及其后面的字符
方法1:去掉空格以及后面的字符 //怎么截取让date的值为"2011/12/9",即去掉空格以及后面的字符 string date = "2011/12/9 2 ...
- MySQL将查询结果写入到文件的2种方法
1.SELECT INTO OUTFIL: 这种方法不能覆盖或者追加到已经存在的文件,只能写入到新文件,并且建立文件的路径需要mysql进程用户有权限建立新文件. mysql 61571 60876 ...
- 并发新构件之PriorityBlockingQueue:优先阻塞队列
PriorityBlockingQueue:优先阻塞队列:是带有优先级的阻塞队列,一个无界阻塞队列,它使用与类 PriorityQueue 相同的顺序规则,并且提供了阻塞获取操作.虽然此队列逻辑上是无 ...
- C# WinfForm 控件之dev报表 XtraReport (七)报表合并
这个不是太难,新建两个报表 一个form窗体 窗体上放个documentView 代码如下 XtraReport2 xr2 = new XtraReport2(); xr2.CreateDocume ...
- 用户态和内核态&操作系统
用户态和内核态 内核态:cpu可以访问内存的所有数据,包括外围设备,例如硬盘,网卡,cpu也可以将自己从一个程序切换到另一个程序. 用户态:只能受限的访问内存,且不允许访问外围设备,占用cpu的能力被 ...