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 的代码。

最新文章

  1. js 闭包之一
  2. css知多少(1)——我来问你来答
  3. 今天工作遇到要发短信(ios)的功能,于是随手记录了一下
  4. 二十六个月Android学习工作总结
  5. Win8.1OS64位oracle11安装配置及PL/SQL Developer怎样连接64位oracle
  6. iOS开发关于AppStore程序的上传流程
  7. 【JAVAWEB学习笔记】27_Redis:在Linux上的安装、Jedis和常用命令
  8. 用于 C&amp;sharp; 图像识别的轮廓分析技术
  9. 学而精计算机公共基础学习之路TEST1
  10. freeplane使用指南
  11. asp.net 经常用到需要判断文本框是否输入的数字是小数,有无正负,几位小数,可以封装一起判断
  12. 集训队日常训练20181201 E 1005 : 小蝌蚪
  13. python 解除装饰器,调用原本函数。
  14. write RE validation
  15. HoloLens开发手记 - 使用HoloLens模拟器 Using HoloLens emulator
  16. 沉迷Link-Cut tree无法自拔之:[BZOJ3514] Codechef MARCH14 GERALD07 加强版
  17. LeetCode(57):插入区间
  18. 前端 HTML 标签里 特殊符号
  19. uva 10163 Storage Keepers
  20. keydown,keypress,keyup三者之间的区别

热门文章

  1. 【bzoj2142】【礼物】拓展Lucas定理+孙子定理
  2. 【R笔记】日期处理
  3. Mybatis添加用户&amp;&amp;Mybatis添加用户返回ID
  4. 静态html分页
  5. 关于JS中原型链中的prototype与_proto_的个人理解与详细总结
  6. 【Node.js】1.安装步骤
  7. Spark createDirectStream 维护 Kafka offset(Scala)
  8. 输入N,打印如图所看到的的三角形(例:N=3,N=4,N=5)1&amp;lt;=N&amp;lt;=26
  9. Java实现图片裁剪预览功能
  10. python基础语法(一)