Description

There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of its moves, judge if this robot ends up at (0, 0) after it completes its moves.

The move sequence is represented by a string, and the character moves[i] represents its ith move. Valid moves are R (right), L (left), U (up), and D (down). If the robot returns to the origin after it finishes all of its moves, return true. Otherwise, return false.

Note: The way that the robot is "facing" is irrelevant. "R" will always make the robot move to the right once, "L" will always make it move left, etc. Also, assume that the magnitude of the robot's movement is the same for each move.

Example:

Input: "UD"
Output: true
Explanation: The robot moves up once, and then down once. All moves have the same magnitude, so it ended up at the origin where it started. Therefore, we return true.

分析:

  1. 设置x = 0, y = 0;
  2. 一次检查每一个字符,分别改变x,y的值
  3. 最后查看x、y是否都是0
class Solution {
public boolean judgeCircle(String moves) {
Robot r = new Robot();
char[] chars = moves.toCharArray();
for(char c: chars){
if(c == 'R') r.x++;
else if(c == 'L') r.x--;
else if(c == 'U') r.y++;
else r.y--;
} if(r.x == 0 && r.y == 0) return true;
else return false; } class Robot{
int x;
int y;
public Robot(){
this.x = 0;
this.y = 0;
}
}
}

657. Robot Return to Origin的更多相关文章

  1. 【leetcode】657. Robot Return to Origin

    Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...

  2. 【Leetcode_easy】657. Robot Return to Origin

    problem 657. Robot Return to Origin 题意: solution1: class Solution { public: bool judgeCircle(string ...

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

  4. 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 it ...

  5. LeetCode 657. Robot Return to Origin (C++)

    题目: There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of its ...

  6. 【LeetCode】657. Robot Return to Origin 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 复数求和 counter统计次数 相似题目 参考资料 ...

  7. LeetCode #657. Robot Return to Origin 机器人能否返回原点

    https://leetcode-cn.com/problems/robot-return-to-origin/ 设置 flagUD 记录机器人相对于原点在纵向上的最终位置 flagRL 记录机器人相 ...

  8. LeetCode--Squares of a Sorted Array && Robot Return to Origin (Easy)

    977. Squares of a Sorted Array (Easy)# Given an array of integers A sorted in non-decreasing order, ...

  9. [Swift]LeetCode657. 机器人能否返回原点 | 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. 给 Haproxy 创建日志文件

    背景介绍:默认下的Haproxy配置是不会生成日志文件的,而无运行日志,无法确定系统运行是否流畅,无法提起预判可能发生的故障 创建Haproxy日志文件的步骤如下vi /etc/rsyslog.con ...

  2. Hdoj 1846.Brave Game 题解

    Problem Description 十年前读大学的时候,中国每年都要从国外引进一些电影大片,其中有一部电影就叫<勇敢者的游戏>(英文名称:Zathura),一直到现在,我依然对于电影中 ...

  3. Hdoj 1086.You can Solve a Geometry Problem too 题解

    Problem Description Many geometry(几何)problems were designed in the ACM/ICPC. And now, I also prepare ...

  4. 自学Python3.3-函数分类(内置函数补充)

    自学Python之路-Python基础+模块+面向对象自学Python之路-Python网络编程自学Python之路-Python并发编程+数据库+前端自学Python之路-django 自学Pyth ...

  5. 「SCOI2014」方伯伯的 OJ 解题报告

    「SCOI2014」方伯伯的 OJ 和列队有点像,平衡树点分裂维护即可 但是需要额外用个set之类的对编号查找点的位置 插入完了后记得splay,删除时注意特判好多东西 Code: #include ...

  6. nginx proxy文件编写总结

    upstream.conf upstream api { server 192.168.10.10:8080; server 192.168.10.20:8080;} server{ listen 4 ...

  7. python面向对象中的一些特殊__方法__

    1. __doc__ 表示类的描述信息 class Foo: """ 描述类信息""" def func(self): pass print ...

  8. 文献笔记:Genome-wide associations for birth weight and correlations with adult disease

    该文献纳入了EGG(Early Growth Genetics Consortium)和UK biobank两大数据库,分为欧洲祖先和非欧洲祖先群体.这两个数据用到的样本量分别如下: Early Gr ...

  9. Day033--Python--进程

    什么是进程? 进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础.在早期面向进程设计的计算机结构中,进程是程序的基本执行实体 ...

  10. 洛谷 P1163"银行贷款"(二分)

    传送门 题解: 二分月利率,假设当前判断的月利率为x: 那么如何判断x是大了还是小了呢? 下面来分析一下Check()函数: bool Check(double x) { double tot=a; ...