657. Judge Route Circle【easy】
2024-09-04 15:14:26
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 的代码。
最新文章
- js 闭包之一
- css知多少(1)——我来问你来答
- 今天工作遇到要发短信(ios)的功能,于是随手记录了一下
- 二十六个月Android学习工作总结
- Win8.1OS64位oracle11安装配置及PL/SQL Developer怎样连接64位oracle
- iOS开发关于AppStore程序的上传流程
- 【JAVAWEB学习笔记】27_Redis:在Linux上的安装、Jedis和常用命令
- 用于 C&;sharp; 图像识别的轮廓分析技术
- 学而精计算机公共基础学习之路TEST1
- freeplane使用指南
- asp.net 经常用到需要判断文本框是否输入的数字是小数,有无正负,几位小数,可以封装一起判断
- 集训队日常训练20181201 E 1005 : 小蝌蚪
- python 解除装饰器,调用原本函数。
- write RE validation
- HoloLens开发手记 - 使用HoloLens模拟器 Using HoloLens emulator
- 沉迷Link-Cut tree无法自拔之:[BZOJ3514] Codechef MARCH14 GERALD07 加强版
- LeetCode(57):插入区间
- 前端 HTML 标签里 特殊符号
- uva 10163 Storage Keepers
- keydown,keypress,keyup三者之间的区别
热门文章
- 【bzoj2142】【礼物】拓展Lucas定理+孙子定理
- 【R笔记】日期处理
- Mybatis添加用户&;&;Mybatis添加用户返回ID
- 静态html分页
- 关于JS中原型链中的prototype与_proto_的个人理解与详细总结
- 【Node.js】1.安装步骤
- Spark createDirectStream 维护 Kafka offset(Scala)
- 输入N,打印如图所看到的的三角形(例:N=3,N=4,N=5)1&;lt;=N&;lt;=26
- Java实现图片裁剪预览功能
- python基础语法(一)