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 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++)的更多相关文章
- 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 机器人能否返回原点
https://leetcode-cn.com/problems/robot-return-to-origin/ 设置 flagUD 记录机器人相对于原点在纵向上的最终位置 flagRL 记录机器人相 ...
- 【leetcode】657. Robot Return to Origin
Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...
- 【Leetcode_easy】657. Robot Return to Origin
problem 657. Robot Return to Origin 题意: solution1: class Solution { public: bool judgeCircle(string ...
- 【LeetCode】657. Robot Return to Origin 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 复数求和 counter统计次数 相似题目 参考资料 ...
- 657. Robot Return to Origin
Description There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequenc ...
- 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, ...
- LeetCode算法题-Robot Return to Origin(Java实现)
这是悦乐书的第281次更新,第298篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第149题(顺位题号是657).在2D平面上有一个从位置(0,0)开始的机器人.给定其移 ...
随机推荐
- JIRA使用方法,简易图解
我们公司要用版本控制(SVN)和过程管理(JIRA)相配合开发软件,所以两个都得用喽! JIRA是集项目计划.任务分配.需求管理.错误跟踪于一体的商业软件.JIRA创建的问题类型包 ...
- FFmpeg中几个结构体的意义
AVCodec是存储编解码器信息的结构体,特指一个特定的解码器,比如H264编码器的名字,ID,支持的视频格式,支持的采样率等: AVCodecContext是一个描述编解码器采用的具体参数,比如采用 ...
- [图解tensorflow源码] 线程池模块分析 (CPU thread pool device)
- 1.1《想成为黑客,不知道这些命令行可不行》(Learn Enough Command Line to Be Dangerous)——运行终端
终端是个允许我们运行命令行的程序,运行命令前,先打开它.在MacOS系统上,可以使用macOS应用 Spotlight来打开终端窗口,Spotlight也有其他两种方式触发,一种是键入⌘␣(comma ...
- 实现Redis Cluster并实现Python链接集群
目录 一.Redis Cluster简单介绍 二.背景 三.环境准备 3.1 主机环境 3.2 主机规划 四.部署Redis 4.1 安装Redis软件 4.2 编辑Redis配置文件 4.3 启动R ...
- node vue 开发环境部署时,外部访问页面出现: Invalid Host header 服务器域名访问出现的问题
这是新版本 webpack-dev-server 出于安全考虑, 默认检查 hostname,如果hostname不是配置内的,将中断访问.顾仅存在于开发环境: npm run dev,打包之后不会 ...
- redis 数据库随笔 (一)
redis数据库的基本类型分析: 1.string 最基本的数据类型.只存贮一个值,key-value,最大值存储512M. 创建命令:hmset 读取命令:hget 2.hash 集合,存储为一个 ...
- PHP中上传文件打印错误,错误类型
一般使用$_FILES来进行文件上传时,可以使用$_FILES["file"]["error"]来判断文件上传是否出错.$_FILES["file&q ...
- iOS开发-通过正则表达式进行各种判断银行卡,车牌号,邮箱地址,QQ,身份证,全字母,仅输入字母或数字同时包含大小写字母和数字,仅能输入中文等
/* * 验证银行卡号是否正确 * 车牌号验证 * 检验邮箱地址是否正确 * 手机号中间四位密文显示 * 判断QQ号是否正确(5-11位) * 判断身份证号是否正确(如末位为字母请用“x” ...
- IT程序员每天的困扰:这TM为啥不可以?这TM也行?
如果有对 Python 感兴趣的程序员,可以加我们小助手的QQ:979950755 会免费送 Python 的视频教程噢! 随着IT互联网对社会的影响越来越重要,关乎人类的未来发展进程.所以现在很多媒 ...