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 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.
题目描述:大概意思是求一个点经过若干次移动能否回到原来的位置。
题目分析:我们只需要去判断两个条件:
- 向左移动的次数和向右移动的次数是否相等
- 向上移动的次数是否和向下移动的次数相等
于是我们只需要计算出向左、向右、向上、向下分别走了多少步就行了。
python 代码:
class Solution(object):
def judgeCircle(self, moves):
"""
:type moves: str
:rtype: bool
"""
moves_length = len(moves)
L_count = 0
R_count = 0
U_count = 0
D_count = 0
for i in range(moves_length):
if moves[i] == 'L':
L_count = L_count + 1
elif moves[i] == 'R':
R_count = R_count + 1
elif moves[i] == 'U':
U_count = U_count + 1
elif moves[i] == 'D':
D_count = D_count + 1
if L_count == R_count and U_count == D_count:
return True
else:
return False
C++ 代码:
class Solution {
public:
bool judgeCircle(string moves) {
int moves_length = moves.length();
int L_count = 0;
int R_count = 0;
int U_count = 0;
int D_count = 0;
for(int i = 0; i < moves_length; i++){
if(moves[i] == 'L'){
L_count++;
}
else if(moves[i] == 'R'){
R_count++;
}
else if(moves[i] == 'U'){
U_count++;
}
else if(moves[i] == 'D'){
D_count++;
}
}
if(L_count == R_count && U_count == D_count){
return true;
}
return false;
}
};
LeetCode 657. Robot Return to Origin的更多相关文章
- 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】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)开始的机器人.给定其移 ...
随机推荐
- katalon之web文件上传
参考:https://docs.katalon.com/katalon-studio/docs/webui-upload-file.html#example- 适用范围:tag=input, type ...
- 记一次坑爹的ORA-01152&ORA-01110错误
最近做RMAN还原时遇到了一次非常坑爹的ORA-01152&ORA-01110错误.遇到的具体错误信息如下所示: RMAN> alter database open resetlogs; ...
- java----OO的概念和设计原则(转)
一.OO(面向对象)的设计基础 面向对象(OO):就是基于对象概念,以对象为中心,以类和继承为构造机制,充分利用接口和多态提供灵活性, 来认识.理解.刻划客观世界和设计.构建相应的软件系统.面向对象的 ...
- C#-运算符(四)
算术运算符 +:两个操作数相加,例:2+3得5 -:第一个操作数减去第二个操作数 例:5-3得2 *:两个操作数相乘,例:2*3得6 /:分子除以分母,例:5/2得2 %:取模运算符,整除后的余数,例 ...
- Windows Server 2016-Powershell管理站点复制
对于Active Directory的Windows PowerShell包括管理复制.网站.域和森林,域控制器以及分区的能力.例如Active Directory的站点和服务管理单元和repadmi ...
- lua时间戳和日期转换及踩坑
介绍lua的日期函数常用方法及我的一个踩坑. 时间戳转日期 os.date("%Y%m%d%H",unixtime) --os.date("%Y%m%d%H", ...
- Java(Java SE7) 体系结构图
原文:https://docs.oracle.com/javase/7/docs/
- 5. svg学习笔记-坐标系变换
之前我们编写图形元素的时候,编写好了位置大小就是固定的,通过坐标系变换,可以移动缩放,旋转图形,但必须声明的是,进行变换时是图形相对于坐标系的变化,就是图形是不发生变化的,而是坐标系发生了变化,比如缩 ...
- CSS多行文本垂直居中
今天需要将文本垂直居中,就是一行是垂直居中,多行也是垂直居中. 效果如下 实现代码(同事提供) <!DOCTYPE html> <html> <head lang=&qu ...
- Set replication in Hadoop
I was trying loading file using hadoop API as an experiment. I want to set replication to minimum as ...