题目标签: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 (困于环中的机器人)的更多相关文章

  1. 【LeetCode】1041. Robot Bounded In Circle 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 找规律 日期 题目地址:https://leetco ...

  2. 【leetcode】1041. Robot Bounded In Circle

    题目如下: On an infinite plane, a robot initially stands at (0, 0) and faces north.  The robot can recei ...

  3. 1041. Robot Bounded In Circle

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

  4. leetcode 1041. 困于环中的机器人

    题目地址 : https://leetcode-cn.com/problems/robot-bounded-in-circle/ 在无限的平面上,机器人最初位于 (0, 0) 处,面朝北方.机器人可以 ...

  5. leetcode 1041——困于环中的机器人

    描述: 在无限的平面上,机器人最初位于 (0, 0) 处,面朝北方.机器人可以接受下列三条指令之一: "G":直走 1 个单位 "L":左转 90 度 &quo ...

  6. 【LeetCode】657. Judge Route Circle 解题报告

    [LeetCode]657. Judge Route Circle 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/judge-route- ...

  7. LeetCode 489. Robot Room Cleaner

    原题链接在这里:https://leetcode.com/problems/robot-room-cleaner/ 题目: Given a robot cleaner in a room modele ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. Yii2 使用十一 在设置enablePrettyUrl时候,defaultAction的设置方法

    启用美化Url的功能 'urlManager' => [ 'enablePrettyUrl' => true, 'showScriptName' => false, 'enableS ...

  2. Shell基础(四):字符串截取及切割、字符串初值的处理、基使用Shell数组、expect预期交互、使用正则表达式

    一.字符串截取及切割 目标: 使用Shell完成各种Linux运维任务时,一旦涉及到判断.条件测试等相关操作时,往往需要对相关的命令输出进行过滤,提取出符合要求的字符串. 本案例要求熟悉字符串的常见处 ...

  3. mac 堡垒机传文件

    安装zssh brew install zssh 上传文件 zssh登陆上跳板机 在跳板机上ssh到相应服务器 在服务器上cd至相应要放上传文件的目录 rz -bye //在远程服务器的相应目录上运行 ...

  4. ASP.NET Core学习——2

    Application Startup ASP.NET Core为应用程序提供了处理每个请求的完整控制.Startup类是应用程程的入口(entry point),这个类可以设置配置(configur ...

  5. CPUID读取有关Cache的信息

    1: void cpuidTest() 2: { 3: u32 val_eax, val_ebx, val_ecx, val_edx; 4: asm("cpuid" 5: : &q ...

  6. 由dubbo开始看看所谓的软负载均衡

    待总结 我们在微服务架构中,常用一些注册中心进行订阅消费我们的服务,这时候对于同一服务请求会有不同的机器同时可以提供服务,这时是怎么选择哪一台机器去连接获取服务呢? 负载均衡设备作为纵跨网络2/7层交 ...

  7. 【洛谷】P1288 取数游戏II

    题目链接:https://www.luogu.org/problemnew/show/P1288 题意:中文题面不赘述啦. 题解:代码很好写,其实就是判断边数是否为偶数.先手确定方向其实都是一样的,但 ...

  8. vue中wath的源码实现

    前言 阅读本节,需要理解vue的数据驱动原理. 看这样一段代码 new Vue({ data: { msg: 'hello', say: 'hello world', }, watch: { msg( ...

  9. pytest--fixture之参数化

    场景:测试用例执行时,有的用例需要登陆才能执行,有些用例 不需要登陆.setup和teardown无法满足.fixture可以.默认 scope(范围)function • 步骤: 1. 导入pyte ...

  10. 2019-9-18-WPF-客户端开发需要知道的触摸失效问题

    title author date CreateTime categories WPF 客户端开发需要知道的触摸失效问题 lindexi 2019-09-18 15:30:38 +0800 2019- ...