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 ...
随机推荐
- Yii2-rbac
下面介绍一个 yii2.0 的 Rbac 权限设置,闲话少说,直接上代码, 1.首先我们要在组件里面配置一下 Rbac ,如下所示(common/config/main-local.php或者ma ...
- 【LeetCode 12】整数转罗马数字
题目链接 [题解] (涨知识了..原来罗马数字是这么回事.. 把{1,4,5,9,10,40,50,90,100,400,500,900,1000}这些东西的罗马数字放在一个数组里面. 每次从大到小减 ...
- docker组件如何协作(7)
还记得我们运行的第一个容器吗?现在通过它来体会一下 Docker 各个组件是如何协作的. 容器启动过程如下: Docker 客户端执行 docker run 命令. Docker daemon 发现本 ...
- HDU-4825 Xor Sum(字典树求异或最大值)
题目链接:点此 我的github地址:点此 Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整 ...
- php开发面试题---创建型设计模式1(创建型设计模式有哪几种)
php开发面试题---创建型设计模式1(创建型设计模式有哪几种) 一.总结 一句话总结: 共五种:(简单工厂模式).工厂方法模式.抽象工厂模式.单例模式.建造者模式.原型模式. 1.学设计模式最好的方 ...
- 3. 初识jmeter及JDK安装
jmeter 介绍 Apache JMeter™应用程序是开源软件,100%纯Java应用程序,旨在加载测试功能行为和测量性能.它最初是为测试Web应用程序而设计的,但后来扩展到其他测试功能. Jme ...
- Vim多窗口编辑
在Linux中使用vim编辑多个窗口 方式: 1. vim -o file1 file2 打开的两个文件上下窗口分布 比如当前目录有makefil ...
- c程序查找字符出现次数
#include <stdio.h> int main(){ char str[100],ch,M,Empty; int i, frequency = 0; fgets(str, (siz ...
- C# WinfForm 控件之dev图表 ChartControl
dev 图表控件 学习连接 新建一个winformApp form1上放一个button 再放一个chartControl Name 为cct 直接上代码 private void button1_C ...
- [POI2011]IMP-Party
题目 不难发现\(\frac{2}{3}n-\frac{1}{3}n=\frac{1}{3}n\)(雾 一个团要求点之间两两有边,于是我们枚举两个点,如果这两个点之间没有边相连,那么就删掉这两个点,由 ...