Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot makes a circle, which means it moves back to the original place.

The move sequence is represented by a string. And each move is represent by a character. The valid robot moves are R (Right), L(Left), U (Up) and D (down). The output should be true or false representing whether the robot makes a circle.

Example 1:

Input: "UD"
Output: true

Example 2:

Input: "LL"
Output: false

这道题让我们判断一个路径是否绕圈,就是说有多少个U,就得对应多少个D。同理,L和R的个数也得相等。这不就是之前那道Valid Parentheses的变种么,这次博主终于举一反三了!这比括号那题还要简单,因为括号至少还有三种,这里就水平和竖直两种。比较简单的方法就是使用两个计数器,如果是U,则cnt1自增1;如果是D,cnt1自减1。同理,如果是L,则cnt1自增1;如果是R,cnt1自减1。最后只要看cnt1和cnt2是否同时为0即可,参见代码如下:

解法一:

class Solution {
public:
bool judgeCircle(string moves) {
int cnt1 = , cnt2 = ;
for (char move : moves) {
if (move == 'U') ++cnt1;
else if (move == 'D') --cnt1;
else if (move == 'L') ++cnt2;
else if (move == 'R') --cnt2;
}
return cnt1 == && cnt2 == ;
}
};

下面这种解法使用了哈希表来建立字符和其出现的次数之间的映射,最后直接比较对应的字符出现的次数是否相等即可,参见代码如下:

解法二:

class Solution {
public:
bool judgeCircle(string moves) {
unordered_map<char, int> m;
for (char c : moves) ++m[c];
return m['L'] == m['R'] && m['U'] == m['D'];
}
};

类似题目:

Valid Parentheses

参考资料:

https://discuss.leetcode.com/topic/99256/c-counter-4-lines-solution

 

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Judge Route Circle 判断路线绕圈的更多相关文章

  1. LeetCode Judge Route Circle

    原题链接在这里:https://leetcode.com/problems/judge-route-circle/description/ 题目: Initially, there is a Robo ...

  2. Judge Route Circle --判断圆路线

    Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot m ...

  3. Leetcode#657. Judge Route Circle(判断路线成圈)

    题目描述 初始位置 (0, 0) 处有一个机器人.给出它的一系列动作,判断这个机器人的移动路线是否形成一个圆圈,换言之就是判断它是否会移回到原来的位置. 移动顺序由一个字符串表示.每一个动作都是由一个 ...

  4. 【LeetCode】657. Judge Route Circle 解题报告

    [LeetCode]657. Judge Route Circle 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/judge-route- ...

  5. 657. Judge Route Circle【easy】

    657. Judge Route Circle[easy] Initially, there is a Robot at position (0, 0). Given a sequence of it ...

  6. LeetCode - 657. Judge Route Circle

    Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot m ...

  7. 657. Judge Route Circle机器人能否返回

    [抄题]: Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this r ...

  8. Judge Route Circle

    Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot m ...

  9. 657. Judge Route Circle

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

随机推荐

  1. maven库

    1.本地仓库 本地仓库是你本地的一个山寨版,只有你看的到,主要起缓存作用. 当你向仓库请求插件或依赖的时候,会先检查本地仓库里是否有.如果有则直接返回,否则会向远程仓库请求,并做缓存. 本地仓库默认在 ...

  2. xilinx和altera复位电平

    xilinx使用高电平复位 altera使用低电平复位 原因:Xilinx 寄存器的SR控制端是高电平有效的.如果RTL代码采用了低电平有效的复位模式,综合器将在复位信号驱动寄存器SR控制端之前的插入 ...

  3. 在Python中进行JSON转化

    序列化,指的是把内存中的变量(如类的实例)变成可存储或可传输的过程. JSON(JavaScript Object Notation, JavaScript对象表示)是网络传输中经常使用的一种数据形式 ...

  4. Python从菜鸟到高手(1):数字

    本文主要内容: 1. 数字的基础知识 2. 大整数 3. 二进制.八进制和十六进制 4 数字的格式化输出 一.数字的基础知识 Python语言与其他编程语言一样,也支持四则运算(加.减.乘.除),以及 ...

  5. JavaEE Servlet 核心方法及生命周期

    做JavaWeb开发,免不了要和Servlet打交道.Servlet是Sun(Oracle)官方定义的一个Web开发规范,所有Servlet开发都必须遵守.自己以前也没有从头做过Web开发,所以这方面 ...

  6. 简单谈谈DNS的工作原理及实践

    DNS协议简介 dns(Domain Name System)是一个全球化的分布式数据库系统,用于存储域名和互联网IP地址的映射关系.dns协议是计算机协议栈应用层中,应用最广泛的协议之一.用户每一次 ...

  7. Java基础学习笔记九 Java基础语法之this和super

    构造方法 我们对封装已经有了基本的了解,接下来我们来看一个新的问题,依然以Person为例,由于Person中的属性都被private了,外界无法直接访问属性,必须对外提供相应的set和get方法.当 ...

  8. hibernate框架学习笔记11:Criteria查询详解

    创建实体类对象: package domain; import java.util.HashSet; import java.util.Set; //客户实体 public class Custome ...

  9. 『备注』&#x; 格式 的编码转换

    在很多 网站(或者很多 WebService), 我们总能看到 Ӓ &#A22A;  这种格式 的编码. 如何将这种编码 转换成 实际文本,C#代码如下: //各种 幺蛾子网页图标 请参见: ...

  10. C语言-最后一次作业

    1.当初你是如何做出选择计算机专业的决定的? 经过一个学期,你的看法改变了么,为什么? 你觉得计算机是你喜欢的领域吗,它是你擅长的领域吗? 为什么? 我当初选择计算机专业是因为我是真的很向往计算机这方 ...