【leetcode】657. Robot Return to Origin
Algorithm
【leetcode】657. Robot Return to Origin
https://leetcode.com/problems/robot-return-to-origin/
1)problem
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.
机器人从位置(0,0)开始,在2D平面上开始。给定一系列动作,判断该机器人在完成动作后是否在(0,0)结束。
移动序列由字符串表示,字符move [i]表示其第i个移动。有效移动是R(右),L(左),U(上)和D(下)。如果机器人在完成所有移动后返回原点,则返回true。否则,返回false。
注意:机器人“面对”的方式无关紧要。“R”将始终使机器人向右移动一次,“L”将始终向左移动等。此外,假设每次移动机器人的移动幅度相同。
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.
机器人向左移动两次。它最终在原点的左边有两个“移动”。我们返回false,因为它不是在它移动结束时的原点。
2)answer
只需要两个变量记录水平方向和垂直方向是否最后处在原点即可;
3)solution
#include "pch.h"
#include <iostream>
#include <string>
using std::string;
class Solution {
public:
bool judgeCircle(string moves) {
int y = 0;
int x = 0;
for (int i = 0; i<moves.length();i++)
{
switch (moves.at(i))
{
case 'U':{ y++; } break;
case 'D':{ y--; } break;
case 'L':{ x--; } break;
case 'R': {x++; } break;
default:
break;
}
}
if (x == 0 && y == 0)
{
return true;
}
return false;
}
};
int main()
{
std::cout << "Hello World!\n";
Solution s;
s.judgeCircle("UD");
}
【leetcode】657. Robot Return to Origin的更多相关文章
- 【LeetCode】657. Robot Return to Origin 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 复数求和 counter统计次数 相似题目 参考资料 ...
- 【Leetcode_easy】657. Robot Return to Origin
problem 657. Robot Return to Origin 题意: solution1: class Solution { public: bool judgeCircle(string ...
- 【LeetCode】657. Judge Route Circle 解题报告
[LeetCode]657. Judge Route Circle 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/judge-route- ...
- LeetCode算法题-Robot Return to Origin(Java实现)
这是悦乐书的第281次更新,第298篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第149题(顺位题号是657).在2D平面上有一个从位置(0,0)开始的机器人.给定其移 ...
- 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 ...
- 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 ...
- 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 ...
- LeetCode #657. Robot Return to Origin 机器人能否返回原点
https://leetcode-cn.com/problems/robot-return-to-origin/ 设置 flagUD 记录机器人相对于原点在纵向上的最终位置 flagRL 记录机器人相 ...
- 【LeetCode】1041. Robot Bounded In Circle 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 找规律 日期 题目地址:https://leetco ...
随机推荐
- CodeForces992E 二分 + 树状数组(线段树)
http://codeforces.com/problemset/problem/992/E 题意:给定一个序列 ai ,记其前缀和序列为 si ,有 q 个询问,每次单点修改,询问是否存在一个 ...
- python3 rrdtool 使用
源自 python自动化运维:技术与最佳实践 并做略微修改 安装 yum install python-rrdtoolyum install rrdtool-devel #因为采集用了psutil模块 ...
- 解决gitk显示文件内容中文乱码
解决gitk显示文件内容中文乱码 1.git config 命令 设置git gui的界面编码 git config --global gui.encoding utf-8 2.修改配置文件 在~\e ...
- NGUI使用图集的精灵换图片
创建了一个sprite,选择的是某一个图集下的sprite,现在我想点击它来换图片.前提是你的sprite已经加了UIButton 代码如下: public void OnClick() { if ( ...
- 5、JPA-映射-单向多对一
多个订单对应一个用户 实体类 Customer package com.jpa.yingshe; import javax.persistence.*; @Table(name = "JPA ...
- JAVA核心技术I---JAVA基础知识(查漏补缺private,static)
一:private对于类和对象(同C++) private是只有这个类内部可以访问(类的成员函数和定义初始化) private是类之间的限制,而不是对对象的限制<重点> 同类对象是可以直接 ...
- Mac下显示网页全屏快捷键
control+command+F mac下谷歌浏览器全屏时隐藏头部:(隐藏标签页和地址栏) command+shift+B
- Java 微信公众号迁移
背景:公众号换主体,要迁移,粉丝(openId)的业务数据要做处理. 第一步:参照我的另一篇文章,Java 导出微信公众号粉丝. 第二部:数据处理(master-worker模式) 程序主入口:Mai ...
- Golang入门教程(十一)beego 框架之RESTful Controller 路由
官方文档:https://beego.me/docs/mvc/controller/router.md 什么是路由设置呢?前面介绍的 MVC 结构执行时,介绍过 beego 存在三种方式的路由:固定路 ...
- PHP7 学习笔记(十一)使用phpstudy快速配置一个虚拟主机
说明:为了windows本地开发php方便,这里推荐使用PHP集成环境phpstudy. 目的:使用域名访问项目(tinywan.test) 1.官网:http://www.phpstudy.net ...