题目标签: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. v-model 双向数据绑定以及修饰符

    <!--v-model 实现双向数据绑定 其中一个值发生改变,另一个值也将实时发生改变--> <div id="app09"> <h1>{{ m ...

  2. Dart编程实例 - Dynamic 关键字

    Dart编程实例 - Dynamic 关键字 void main() { dynamic x = "tom"; print(x); } 本文转自:http://codingdict ...

  3. 思维+贪心——cf1042D

    /* 首先考虑从后往前计算lis,显然0的在很多情况下的贡献要大于1 如果遇上0,那么lis++,如果遇上1,那么cnt1++,并且用cnt1更新lis 这样的贪心保证正确,因为从[i,j]这一段的l ...

  4. 高级运维(一):反向代理&使用Varnish加速Web

    案例1.反向代理      目标: 1.代理服务器可以将远程的Web服务器页面缓存于本地 2.代理服务器端口设置为80端口 3.用户通过访问代理服务器即可获得远程Web服务器页面上的内容 4.远程We ...

  5. 找关键字,分割字符串,输出一个vector

    vector<string> split(const string& str, const string& delim) { vector<string> re ...

  6. 51nod-1515 明辨是非——并查集

    给n组操作,每组操作形式为x y p. 当p为1时,如果第x变量和第y个变量可以相等,则输出YES,并限制他们相等:否则输出NO,并忽略此次操作. 当p为0时,如果第x变量和第y个变量可以不相等,则输 ...

  7. psql内部命令及对应sql语句

    \?: 查看所有帮助  \l: 查看所有数据库  SELECT d.datname as "Name",       pg_catalog.pg_get_userbyid(d.da ...

  8. 记Selenium HTMLTestRunner 无法生成测试报告的总结

      使用Python ,HTMLTestRunner 生成测试报告时,遇到很奇怪的问题,明明运行的结果,没有任何报错,就是不生成测试报告,纠结好久.google+baidu搜索结果也不满意,最后终于解 ...

  9. f-li.cn

    package org.rx.service.command.impl; import lombok.Getter; import lombok.Setter; import org.rx.core. ...

  10. Dubbo入门到精通学习笔记(十四):ActiveMQ集群的安装、配置、高可用测试,ActiveMQ高可用+负载均衡集群的安装、配置、高可用测试

    文章目录 ActiveMQ 高可用集群安装.配置.高可用测试( ZooKeeper + LevelDB) ActiveMQ高可用+负载均衡集群的安装.配置.高可用测试 准备 正式开始 ActiveMQ ...