【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 ...
随机推荐
- 【知名的移动APP和网站设计工具】Sketch for Mac 54.1
以上图片来源于互联网分享,如涉及版权问题请联系作者删除. 文章素材来源:风云社区(www.scoee.com) 下载地址:风云社区(www.scoee.com) [简介] Sketch 是一款适用 ...
- 增删改查的SSM小项目
经过将近一个月的摸索,终于算是勉强完成了关于增删改查的SSM项目. github源码地址:https://github.com/123456abcdefg/Login 好了,话不多说,写一下具体代 ...
- C语言复习---输出魔方阵
一:奇魔方阵 算法: 1.第一个元素放在第一行中间一列 .下一个元素存放在当前元素的上一行.下一列. .如果上一行.下一列已经有内容,则下一个元素的存放位置为当前列的下一行. 在找上一行.下一行或者下 ...
- golang命令行参数
os.Args获取命令行参数 os.Args是一个srting的切片,用来存储所有的命令行参数 package main import ( "fmt" "os" ...
- Part-One
首先,必须要声明一下,这个目录下的所有东西,是我对一本书复习,只是敲出部分代码让自己不至于眼高手低,其中有很多东西可能都是我的个人理解,如果有兴趣的朋友可以看一下,同时也欢迎大家指正. 1.Hello ...
- 前端面试题整理—HTML/CSS篇
1.简述一下你对HTML语义化的理解 1)用正确的标签做正确的事情 2)html语义化让页面的内容结构化,结构更清晰,便于对浏览器.搜索引擎解析 3)即使在没有样式CSS情况下也以一种文档格式显示,并 ...
- jspdf简单使用
安装 npm install jspdf --save 英文输出 import jsPDF from 'jspdf-customfonts' let doc = new jsPDF() doc.tex ...
- Mac 开发使用中的小技巧收集
1. mac 下ssh连接到 linux 服务器管理,同putty,无需第三方 Mac 下打开终端,输入: ssh 登录用户名@ip地址 如: ssh root@142.138.1.89 如有询问是否 ...
- Devexpress Winform 使用MVVM
MVVM在WPF里很早就有了,在Winform里Devexpress最近几个大版本才有的事,上一段代码. 现在对话框上添加三个控件simpleButton1,simpleButton2,textEdi ...
- 求f(n)=[n/1]+[n/2]+---+[n/n]的值 简单杂题
这种小题首先根据 n/1+n/2+n/3+--+n/n=nlogn+欧拉常数r 可以知道 1e12的范围也不会爆longlong,不需要写高精度(到现在都不会写) 再根据数据范围可知O(n)级别的暴力 ...