题目:

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 1:

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.

Example 2:

Input: "LL"
Output: false
Explanation: The robot moves left twice. It ends up two "moves" to the left of the origin. We return false because it is not at the origin at the end of its moves.

分析:

这道题很简单,有一个机器人站在(0,0)的位置,有四个动作分别是R (right), L (left), U (up), and D (down)。在经过一系列动作后是否在(0,0)的位置。

可以定义两个值来代表垂直方向和水平方向的值,初始值是(0,0),R就水平方向+1,L则相反,U和D同理。最后判断两个方向的值是否都为0即可。

程序:

class Solution {
public:
bool judgeCircle(string moves) {
int res[] = {};
for (int i = ; i < moves.length(); i++){
if (moves[i] == 'U')
res[] += ;
if (moves[i] == 'D')
res[] -= ;
if (moves[i] == 'R')
res[] += ;
if (moves[i] == 'L')
res[] -= ;
}
if ((res[] == ) && (res[] == ))
return true;
else
return false;
}
};

LeetCode 657. Robot Return to Origin (C++)的更多相关文章

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

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

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

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

  4. 【leetcode】657. Robot Return to Origin

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

  5. 【Leetcode_easy】657. Robot Return to Origin

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

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

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

  7. 657. Robot Return to Origin

    Description There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequenc ...

  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. LeetCode算法题-Robot Return to Origin(Java实现)

    这是悦乐书的第281次更新,第298篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第149题(顺位题号是657).在2D平面上有一个从位置(0,0)开始的机器人.给定其移 ...

随机推荐

  1. gitblit server windows搭建

    环境配置: windows64bit 系统 jdk1.8安装配置环境变量,这个不说了百度有 gitblit官网下载对应操作系统位数64bit包解压:http://www.gitblit.com/ ​ ...

  2. P1070 道路游戏

    题目描述 小新正在玩一个简单的电脑游戏. 游戏中有一条环形马路,马路上有 n 个机器人工厂,两个相邻机器人工厂之间由一小段马路连接.小新以某个机器人工厂为起点,按顺时针顺序依次将这 n 个机器人工厂编 ...

  3. HDU 2141 Can you find it? (二分)

    题目链接: Can you find it? Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/10000 K (Java/ ...

  4. 关于mysql中GROUP_CONCAT函数的使用

    偶然看到公司存储过程中有个字符串拼接的函数,改bug过程中使用到了,还挺有用的,于是记录下来方便记忆,帮助有需要的人. 这是我需要整理的串,他是调用了一个存储过程,传入组织机构的id和迭代层数,返回来 ...

  5. 为什么继续选择DELPHI?

    已经钻DELPHI很深了,当然现在DELPHI是过了最辉煌的时代.但为什么要继续下去,而不转向其它的?这是不是死脑筋? 我看了一下C#的LINQ的产生,然后又被实体框架所代替.思考了一下: 1)LIN ...

  6. Rust 智能指针(一)

    Rust 智能指针(一) 1.Box<T> Box<T>是指向堆中的指针. fn main() { let box = Box::new(3); println!(" ...

  7. 生死系列--SongXingZhi

    SongXingZhi,我上中专时的同学,任劳动委员,湖北随州人.其实在上学以及毕业后,我们俩没什么交往,也没说过几句话. 印象比较深的一次是在上什么课时,老师拖堂了 ,他直接从课桌上翻过来,从后门出 ...

  8. 20155318 2016-2017-2 《Java程序设计》第七学习总结

    20155318 2016-2017-2 <Java程序设计>第七学习总结 教材学习内容总结 学习目标 了解Lambda语法 了解方法引用 了解Fucntional与Stream API ...

  9. 2017-2018-1 20155317 IPC

    2017-2018-1 20155317 IPC - 共享内存 共享内存主要是通过映射机制实现的.以window系统调用为例子:Windows 下进程的地址空间在逻辑上是相互隔离的,但在物理上却是重叠 ...

  10. JavaScript之字符串的常用操作函数

    字符串的操作在js中非常繁琐,但也非常重要.在使用过程中,也会经常忘记,今天就对这个进行一下整理. String 对象 String 对象用于处理文本(字符串). new String(s); // ...