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)开始的机器人.给定其移 ...
随机推荐
- PATtest1.3:最大子列和
题目源于:https://pintia.cn/problem-sets/16/problems/663 题目要求:输入一个数列,求其最大子列和. 问题反馈:1.部分C++代码不是很熟练 2.没有仔细读 ...
- 如何将freemarker文件转化为html文件
最近在做静态的页面报表服务,将前端生成的ftl文件转化为html格式的文件,供后面合成pdf使用. freemarker基础可以参见:freemarker官方文档 前期准备:需要一个基础的ftl格式的 ...
- 443 B. Table Tennis
http://codeforces.com/contest/879/problem/B n people are standing in a line to play table tennis. At ...
- 五种典型开发周期模型(瀑布、V、原型化、螺旋、迭代)
五种典型开发周期模型(瀑布.V.原型化.螺旋.迭代) 总结一下经常可以见到的系统开发周期模型. 在过去的几年里,可以很奇葩的碰到类似于“创业项目库”这种需求非常明确,工作量十分可控,对质量要求比 ...
- modbustcp封装使用获取设备数据示例
//接受数据请求public function client($pz){ //参数1是:网络协议, //AF_INET: IPv4 网络协议.TCP 和 UDP 都可使用此协议.一般都 ...
- Liunx信息显示与文件搜索
. uname 显示系统相关信息,如内核版本号,硬件架构 -a # 显示系统所有相关信息 -m # 显示计算机硬件架构 -n # 显示主机名称 -r # 显示内核发行版本号 -s # 显示内核名称 - ...
- 微信公众号发送消息模板(java)
这段时间接触公众号开发,写下向用户发送消息模板的接口调用 先上接口代码 public static JSONObject sendModelMessage(ServletContext context ...
- 大数据入门:Maven项目的创建及相关配置
目录 Maven项目的创建及相关配置 一.Maven的介绍 1.Maven是什么: 2.Maven作用: 3.Maven项目的目录结构: 4.Maven的三点坐标: 5.maven的pom文件: 6. ...
- 【转】使用nginx搭建高可用,高并发的wcf集群
原文:http://www.cnblogs.com/huangxincheng/p/7707830.html 很多情况下基于wcf的复杂均衡都首选zookeeper,这样可以拥有更好的控制粒度,但zk ...
- SWT开发工具
http://www.eclipse.org/swt/tools.php Eclipse有很多的透视图,比如Debug,或者java.下面分别是Debug,和java的透视图,可以发现,他们的结构不一 ...