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)开始的机器人.给定其移 ...
随机推荐
- Docker Data Center系列(五)- 使用自定义的TLS安全认证
本系列文章演示如何搭建一个mini的云平台和DevOps实践环境. 基于这套实践环境,可以部署微服务架构的应用栈,演练提升DevOps实践能力. 1 名词说明 CSR: Certificate Sig ...
- hadoop java上传文件
import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.InputStream; impo ...
- [20181225]12CR2 SQL Plan Directives.txt
[20181225]12CR2 SQL Plan Directives.txt --//12C引入SQL PLAN Directives.12cR1版本会造成大量的动态取样,影响性能.许多人把OPTI ...
- web前端(3)—— html标签及web页面结构
本节内容简单介绍下html都有哪些标签 还是百度首页,查看源代码看看: 我把源代码复制下来另存为html文件里: 注意:网页文件的后缀都是html或者htm 我这用的pycharm编辑器(Python ...
- C# 枚举转列表
using System; using System.Collections.Generic; using System.ComponentModel; namespace Common.Utils ...
- jQuery -- 光阴似箭(二):jQuery效果的使用
jQuery -- 知识点回顾篇(二):jQuery效果的使用 1. jQuery 效果 -- 隐藏和显示: 使用 hide() 和 show() 方法来隐藏和显示 HTML 元素. <!DO ...
- vue中父组件调用子组件函数
用法: 子组件上定义ref="refName", 父组件的方法中用 this.$refs.refName.method 去调用子组件方法 详解: 父组件里面调用子组件的函数,父组 ...
- 我使用的Bem的习惯
在基于BEM命名思想的基础上,我整理一些实用的点: BEM,B即block(块),E即element(元素),M即modifier(修饰符) 块:最顶层,可包含块和元素 元素:被块包含,通常为最终被包 ...
- MySQL高级知识(一)——基础
前言:MySQL高级知识主要来自尚硅谷中MySQL的视频资源.对于网上视频资源来说,尚硅谷是一个非常好的选择.通过对相应部分的学习,笔者可以说收益颇丰,非常感谢尚硅谷. 1.关于MySQL的一些文件 ...
- 给定一个正整数n,返回从1到n构成的所有的BST
public class C3 { public static void main(String[] args) { ArrayList<TreeNode> res = generateT ...