题目:

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. error:Assertion failed ((unsigned)i0 < (unsigned)size.p[0]) in cv::Mat::at

    问题原因: 访问像素时指针越界造成的 解决办法: 1.检查指针下标是否正确 2.row和col是否写反了

  2. oracle 基本知识点

    //创建临时表空间create temporary tablespace test_temp tempfile 'E:\oracle\product\10.2.0\oradata\testserver ...

  3. K2 4.7 升级 数据库排序规则更改

    介绍 在过去,K2没有指定安装过程中要在其数据库上使用的标准排序规则.然而,现在K2引入了标准排序规则,以便在之后使用(如果我没有错的话,它是在4.7). 因此, 问题出现在数据库的排序规则不是Lat ...

  4. K8S学习心得 == 安装虚拟路由器RouterOS

    使用RouterOS, 搭建虚拟路由器,并且配置多个网关互通.配置步骤如下.   基础配置 1. RouterOS 服务器,设置如下             2. VM 不同网段的设置 == 192. ...

  5. Qt常用控件

    Qt常用控件 QWidget与QFrame QWidget所有图形控件的基类 QFrame与QWidget的区别 QFrame是基本控件的基类, QWidget是QFrame的基类. 因此QFrame ...

  6. NoSQL入门第二天——Redis入门介绍

    一.基本概述 1.是什么 Redis:REmote DIctionary Server (远程字典服务器) 是完全开源免费的,用C语言编写的,遵守BSD协议, 是一个高性能的(key/value)分布 ...

  7. exLucas学习笔记

    exLucas学习笔记 Tags:数学 写下抛硬币和超能粒子炮改 洛谷模板代码如下 #include<iostream> #define ll long long using namesp ...

  8. python基础学习2-easygui框架编程

    #!/usr/bin/env python # -*- coding:utf-8 -*- import easygui as g #导入方式一 #导入方式2 #from easygui import ...

  9. django学习笔记(4)

    Part 4: Forms and generic views ====> Write a simple form$ edit polls\templates\polls\detail.html ...

  10. 1483: [HNOI2009]梦幻布丁

    1483: [HNOI2009]梦幻布丁 链接 分析: 启发式合并+链表. 代码: #include<cstdio> #include<algorithm> #include& ...