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】的更多相关文章

  1. 【LeetCode】657. Judge Route Circle 解题报告

    [LeetCode]657. Judge Route Circle 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/judge-route- ...

  2. Leetcode#657. Judge Route Circle(判断路线成圈)

    题目描述 初始位置 (0, 0) 处有一个机器人.给出它的一系列动作,判断这个机器人的移动路线是否形成一个圆圈,换言之就是判断它是否会移回到原来的位置. 移动顺序由一个字符串表示.每一个动作都是由一个 ...

  3. 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 ...

  4. 657. Judge Route Circle机器人能否返回

    [抄题]: Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this r ...

  5. 657. Judge Route Circle

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

  6. 520. Detect Capital【easy】

    520. Detect Capital[easy] Given a word, you need to judge whether the usage of capitals in it is rig ...

  7. 680. Valid Palindrome II【easy】

    680. Valid Palindrome II[easy] Given a non-empty string s, you may delete at most one character. Jud ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. java调用windows的wmi获取设备性能数据

    java调用windows的wmi获取监控数据(100%纯java调用windows的wmi获取监控数据) 转:http://my.oschina.net/noahxiao/blog/73163 纯j ...

  2. C#中Math的使用总结

    1.向上进位取整.Math.Ceiling 例如: Math.Ceiling(32.6)=33; Math.Ceiling(32.0)=32; 2.向下舍位取整.Math.Floor 例如: Math ...

  3. ActiveMQ实战-集群

    原文:http://blog.csdn.net/lifetragedy/article/details/51869032 ActiveMQ的集群 内嵌代理所引发的问题: 消息过载 管理混乱 如何解决这 ...

  4. win8 下脚本安装IIS

    @echo off      echo 正在添加IIS8.0 功能,依据不同的网络速率,全程大约需要5分钟时间...      start /w pkgmgr /iu:IIS-WebServerRol ...

  5. Admin Finder

    #Created for coded32 and his teamopenfire Eliminated Some bugs from my last code shared here as Gues ...

  6. javascript快速入门25--浏览器中的XML

    打开XML 首先,直接从浏览器中打开XML文件,浏览器会对其进行格式良好性检查,如果不符合XML语法规范则显示出错,如果格式良好,再检查是否包含样式表(CSS或XSL),如果包含样式表,则用样式表格式 ...

  7. Guice 学习(五)多接口的实现( Many Interface Implementation)

    1.接口 /* * Creation : 2015年6月30日 */ package com.guice.InterfaceManyImpl; public interface Service { p ...

  8. Centos中mount命令挂载windows7共享文件夹

    1)  在ip:10.4.35.77的windows机器上新建用户.这里新建username:myshare,password:myshare123. 选择 [计算机]右键 选择[管理],本地用户和组 ...

  9. Unity异常警告错误处理方法

    原地址:http://www.haogongju.net/art/2591936 1.  The AnimationClip 'cube1_anim' used by the Animation co ...

  10. PHP函数之HTMLSPECIALCHARS_DECODE

    PHP函数之htmlspecialchars_decode   htmlspecialchars_decode :将特殊的 HTML 实体转换回普通字符   htmlspecialchars: 将普通 ...