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. Konckout第二个实例:数组数据类型双向绑定 -- 下拉select

    自定义js做法: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> &l ...

  2. 【数据结构】赫夫曼树的实现和模拟压缩(C++)

    赫夫曼(Huffman)树,由发明它的人物命名,又称最优树,是一类带权路径最短的二叉树,主要用于数据压缩传输. 赫夫曼树的构造过程相对比较简单,要理解赫夫曼数,要先了解赫夫曼编码. 对一组出现频率不同 ...

  3. centos 安装atom 笔记

    一.安装atom  "To install Atom on Linux, you can download a Debian package or RPM package either fr ...

  4. Java基础学习笔记十七 集合框架(三)之Map

    Map接口 通过查看Map接口描述,发现Map接口下的集合与Collection接口下的集合,它们存储数据的形式不同,如下图. Collection中的集合,元素是孤立存在的(理解为单身),向集合中存 ...

  5. python全栈开发-Day12 三元表达式、函数递归、匿名函数、内置函数

    一. 三元表达式 一 .三元表达式 仅应用于: 1.条件成立返回,一个值 2.条件不成立返回 ,一个值 def max2(x,y): #普通函数定义 if x > y: return x els ...

  6. Beta第七天

    听说

  7. Session的过期时间如何计算?

    在生成session的时候,会设置一个session过期时间.session的过期时间并不是从生成session对象开始计算,超过过期时间,session就失效了. 而是每当一个浏览器请求,sessi ...

  8. raid5两块硬盘离线怎么办? 强制上线失败如何恢复数据

    服务器故障描述: 客户使用Dell 2850服务器组建了raid5磁盘阵列,阵列中包含有6块硬盘(SCSI硬盘,单盘容量300G),服务器操作系统为linux Redhat4:文件系统为ext3文件系 ...

  9. 利用Node的chokidar 监听文件改变的文件。

    最近维护一个项目.每次改完东西,都要上传到服务器.然后有时候就忘记一些东西,于是就想有没有可以方法能监听文件的改变.然后我再利用程序把更改的文件一键上传到服务器. 于是就找到了nodejs 的chok ...

  10. machine learning 之 导论 一元线性回归

    整理自Andrew Ng 的 machine learnig 课程 week1. 目录: 什么是机器学习 监督学习 非监督学习 一元线性回归 模型表示 损失函数 梯度下降算法 1.什么是机器学习 Ar ...