[LeetCode] Judge Route Circle 判断路线绕圈
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'];
}
};
类似题目:
参考资料:
https://discuss.leetcode.com/topic/99256/c-counter-4-lines-solution
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Judge Route Circle 判断路线绕圈的更多相关文章
- LeetCode Judge Route Circle
原题链接在这里:https://leetcode.com/problems/judge-route-circle/description/ 题目: Initially, there is a Robo ...
- Judge Route Circle --判断圆路线
Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot m ...
- Leetcode#657. Judge Route Circle(判断路线成圈)
题目描述 初始位置 (0, 0) 处有一个机器人.给出它的一系列动作,判断这个机器人的移动路线是否形成一个圆圈,换言之就是判断它是否会移回到原来的位置. 移动顺序由一个字符串表示.每一个动作都是由一个 ...
- 【LeetCode】657. Judge Route Circle 解题报告
[LeetCode]657. Judge Route Circle 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/judge-route- ...
- 657. Judge Route Circle【easy】
657. Judge Route Circle[easy] Initially, there is a Robot at position (0, 0). Given a sequence of it ...
- 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 ...
- 657. Judge Route Circle机器人能否返回
[抄题]: Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this r ...
- Judge Route Circle
Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot m ...
- 657. Judge Route Circle
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
随机推荐
- WCF配置问题(配置WCF跨域)
其它的先放一边.今天先来分享一下前段时间给公司做网站WCF服务接口的心得. 配置文件的配置问题 这里既然讨论WCF配置文件的问题,那么怎么创建的就不一一讲解了.好多博主都有提过的.所以直接分享自己开发 ...
- 论文阅读——Visual inertial odometry using coupled nonlinear optimization
简介:论文提出一种新的视觉里程计算法,其直接利用带噪声的IMU数据和视觉特征位置来优化相机位姿.不同于对IMU和视觉数据运行分离的滤波器,这种算法将它们纳入联合的非线性优化框架中.视觉特征的透视重投影 ...
- Django—urls系统:urls基础
Django的urls系统简介 Django 1.11版本 URLConf官方文档 URL配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL与要为该URL调用的视图函数之间的映 ...
- 第二次作业-关于Steam游戏平台的简单分析
1.1 Steam平台的简单介绍 你选择的产品是? 如题,这次的作业我选择了Steam作为分析的对象. 为什么选择该产品作为分析? 我选择数字游戏贩售平台STEAM作为分析对象的原因有以下几点: 1. ...
- C语言第二次博客作业---分支结构 陈张鑫
一.PTA实验作业 题目1:计算分段函数[2] 本题目要求计算下列分段函数f(x)的值: 1.实验代码 int main(){double x,y; scanf("%lf",&am ...
- java web 初学
我希望在本学期本堂课上学会使用java web 框架 精通mvc架构模式 学会通过框架和数据库对产品进行构造与编写. 我计划每周用16小时的时间进行学习java web 一周4学时上课时间 周一到周五 ...
- android 自定义ScrollView实现背景图片伸缩(阻尼效果)
android 自定义ScrollView实现强调内容背景图片伸缩(仿多米,qq空间背景的刷新) 看到一篇文章,自己更改了一下bug: 原文地址:http://www.aiuxian.com/arti ...
- 《高级软件测试》Linux平台Jira的安装与配置
现在大部分的程序开发都是在linux下进行的,jira更多的时候是安装在linux上,那么,如何在linux下安装配置jira呢?本文将以Ubuntu 17.10和jira7.5.2为例,对linux ...
- 一个毕生难忘的BUG
记得以前接手过一个Java项目,服务器程序,直接让Jar在linux上跑的那种, 这个项目由两个web服务组成,也就是两条Java进程,主进程 xxx.jar,辅助进程 xxx_helper.jar. ...
- ajax的四种type类型
1.GET请求会向数据库发索取数据的请求,从而来获取信息,该请求就像数据库的select操作一样,只是用来查询一下数据,不会修改.增加数据,不会影响资源的内容,即该请求不会产生副作用.无论进行多少次操 ...