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. python学习日记(文件操作练习题)

    登录注册(三次机会) name = input('请注册姓名:') password = input('请注册密码:') with open('log',mode='w',encoding='utf- ...

  2. 概率DP自学

    转自https://blog.csdn.net/zy691357966/article/details/46776199 zy691357966的blog 有关概率和期望问题的研究 摘要 在各类信息学 ...

  3. Shell 字符截取命令 awk

    awk命令# awk '条件1{动作1} 条件2{动作2}….' 文件名一般使用关系表达式作为条件动作:1.格式化输出 2.流程控制语句 处理cut命令无法截取空格的列 例子:截取出磁盘使用率 df ...

  4. Luogu P4643 【模板】动态dp(矩阵乘法,线段树,树链剖分)

    题面 给定一棵 \(n\) 个点的树,点带点权. 有 \(m\) 次操作,每次操作给定 \(x,y\) ,表示修改点 \(x\) 的权值为 \(y\) . 你需要在每次操作之后求出这棵树的最大权独立集 ...

  5. Android热点回顾第一期

    本文由 ImportNew - 陈强 翻译自 androidweekly.如需转载本文,请先参见文章末尾处的转载要求. Importnew注:欢迎Android爱好者参与翻译文中提及的教程,请私信联系 ...

  6. 【算法】php计算出丑数

    丑数描述 把只包含因子2,3,5的正整数被称作丑数,比如4,10,12都是丑数,而7,23,111则不是丑数. 判断方法    首先除2,直到不能整除为止,然后除5到不能整除为止,然后除3直到不能整除 ...

  7. golang语言并发与并行——goroutine和channel的详细理解(一)

    如果不是我对真正并行的线程的追求,就不会认识到Go有多么的迷人. Go语言从语言层面上就支持了并发,这与其他语言大不一样,不像以前我们要用Thread库 来新建线程,还要用线程安全的队列库来共享数据. ...

  8. 粉红猪小妹peppa pig中英文版209集+218本绘本+音频

    1.avi格式英文版包括,第一季52集有外挂英文字幕,第二季53集外挂英文字幕,第三季52集有外挂同步英文字幕,第四季1到39集有英文字幕,40-52无字幕.另有4季音频(每集都是单独的音频文件,方便 ...

  9. 逗号分隔的字符串与List互转

    将逗号分隔的字符串转换为List // 将逗号分隔的字符串转换为List String str = "a,b,c"; // 1.使用JDK,逗号分隔的字符串-->数组--&g ...

  10. Electron一学习资源收集和练习demo

    1.近日为了做项目查资料学习electron,简直头都要炸了,就官方的electron-quick-start的例子进行了基本的练习之后,不断的查资料终于发现一些有用的demo来看源代码学习,一遍看代 ...