657. Judge Route Circle【easy】
657. Judge Route Circle【easy】
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
解法一:
class Solution {
public:
bool judgeCircle(string moves) {
int h = ;
int w = ;
for (int i = ; i < moves.length(); ++i) {
if (moves[i] == 'U') {
h++;
} else if (moves[i] == 'D') {
h--;
} else if (moves[i] == 'R') {
w++;
} else if (moves[i] == 'L') {
w--;
}
}
return (h == && w == );
}
};
解法二:
class Solution {
public:
bool judgeCircle(string moves) {
unordered_map<char, int> c;
for ( char m : moves )
++c[m];
return c['L'] == c['R'] && c['U'] == c['D'];
}
};
参考@zqfan 的代码。
657. Judge Route Circle【easy】的更多相关文章
- 【LeetCode】657. Judge Route Circle 解题报告
[LeetCode]657. Judge Route Circle 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/judge-route- ...
- Leetcode#657. Judge Route Circle(判断路线成圈)
题目描述 初始位置 (0, 0) 处有一个机器人.给出它的一系列动作,判断这个机器人的移动路线是否形成一个圆圈,换言之就是判断它是否会移回到原来的位置. 移动顺序由一个字符串表示.每一个动作都是由一个 ...
- 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 ...
- 657. Judge Route Circle
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
- 520. Detect Capital【easy】
520. Detect Capital[easy] Given a word, you need to judge whether the usage of capitals in it is rig ...
- 680. Valid Palindrome II【easy】
680. Valid Palindrome II[easy] Given a non-empty string s, you may delete at most one character. Jud ...
- 170. Two Sum III - Data structure design【easy】
170. Two Sum III - Data structure design[easy] Design and implement a TwoSum class. It should suppor ...
- 160. Intersection of Two Linked Lists【easy】
160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...
随机推荐
- 【并查集】bzoj2054 疯狂的馒头
因为只有最后被染上的颜色会造成影响,所以倒着处理,用并查集维护已经染色的区间的右端点,即fa[i]为i所在的已染色区间的右端点,这样可以保证O(n)的复杂度. #include<cstdio&g ...
- python3 开发面试题(装饰器必须考)6.4
def f(): print("2018-06-04") # 每次调用f的时候 在打印"2018-06-04" 之前打印一句 开始, 之后再打印一句 结束 de ...
- catalina_home与catalina_base
CATALINA_HOME是Tomcat的安装目 录,CATALINA_BASE是Tomcat的工作目录. 如果我们想要运行Tomcat的多个实例,但是不想安装多个Tomcat软件副本.那么我们可以配 ...
- python中的else子句
在一般的语言中else子句一般是紧跟在if 子句后面,但是python语言中else子句可以不跟在if子句后面,请看下面代码: >>> for n in range(2, 10): ...
- IntelliJ IDEA下Maven控制台出现中文乱码问题解决
Setting->maven->runner: VMoptions: -Dfile.encoding=GB2312 不一定有效,要不断的尝试.
- Error: Top-level design entity "dff" is undefined
原因是:在quartus库文件里面已将dff定义了,要是找使用这个名字重命名了,因而需要重新命名为其他的名字.
- nginx 实现 ajax 跨域请求
原文:http://www.nginx.cn/4314.html AJAX从一个域请求另一个域会有跨域的问题.那么如何在nginx上实现ajax跨域请求呢?要在nginx上启用跨域请求,需要添加a ...
- CSS3:绘制图形
CSS画图形,基本上所有的实现都是对边框的角度的调整及组合. 以下不包含兼容浏览器的前缀,请使用时在border-radius前加-moz-.-webkit- .... 1.正常得不得了的矩形 .sq ...
- CMD_命令行
一.bat执行一闪而过 最后一个end下一行,加PAUSE 二.cmd命令:不是内部或外部命令,也不是可运行的程序或批处理文件 解决: 需将路径先切换到system32下 cd c:\WINDO ...
- VB6的UTF8编码解码
'UTF-8编码 Public Function UTF8Encode(ByVal szInput As String) As String Dim wch As String D ...