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
public class Solution {
public boolean judgeCircle(String moves) {
if (moves == null)
return true;
int x = 0, y = 0;
for (int i=0; i<moves.length(); i++) {
char ch = moves.charAt(i);
if (ch == 'U')
y ++;
else if (ch == 'D')
y --;
else if (ch == 'L')
x --;
else if (ch == 'R')
x ++;
}
return x==0 && y==0;
}
}

LeetCode - 657. Judge Route Circle的更多相关文章

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

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

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

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

  3. 657. Judge Route Circle【easy】

    657. Judge Route Circle[easy] Initially, there is a Robot at position (0, 0). Given a sequence of it ...

  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. [LeetCode] Judge Route Circle 判断路线绕圈

    Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot m ...

  7. LeetCode Judge Route Circle

    原题链接在这里:https://leetcode.com/problems/judge-route-circle/description/ 题目: Initially, there is a Robo ...

  8. Judge Route Circle

    Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot m ...

  9. Judge Route Circle --判断圆路线

    Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot m ...

随机推荐

  1. idea 远程调试 tomcat web应用

    最近在做的一个东西,测试环境和本地环境差距太大,本地能运行的代码,放到测试环境上到处报错,哪里哪里都连不上,所以决定把代码部署到远程服务器上调试,节省时间. 网上看了很多教程,大部分都是互相抄来抄去, ...

  2. call 和 ret 指令

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  3. Python装饰器小代码

    # coding=utf-8import timedef outer(fun): def inner(): start = time.time() fun() runtime = time.time( ...

  4. 浅谈Android进阶之路

    过去十年是移动互联网蓬勃发展的黄金期,相信每个人也都享受到了移动互联网红利,在此期间,移动互联网经历了曙光期.成长期.成熟期.现在来说已经进入饱和期.依然记得在 2010-2013 年期间,从事移动开 ...

  5. 基于百度地图SDK和Elasticsearch GEO查询的地理围栏分析系统(2)-查询实现

    在上一篇博客中,我们准备好了数据.现在数据已经以我们需要的格式,存放在Elasticsearch中了. 本文讲述如何在Elasticsearch中进行空间GEO查询和聚合查询,以及如何准备ajax接口 ...

  6. MyEclipse中导出javadoc文档

    1.选中要生成文档的类或者项目→File→Export→Java→Javadoc. 2.在Javadoc command中选择JDK下对应的javadoc.exe:Select types for w ...

  7. css盒子居中定位问题

    在HTML中,div盒子的居中要通过外边距margin和width来控制,首先确定盒子的宽度,然后确定盒子方位并将其平移便可使盒子移到固定位置. <div id="divpic&quo ...

  8. Shell 批量复制文件名相近的文件到指定文件名中

    问题: 目录结构如下: test/ 2001.01.01/   2001.02.02/   2001.03.02/ 2001.01.03/    2001.02.04/   2001.03.05/ 2 ...

  9. 【转】GLONASS全球卫星导航系统

    GLONASS是“GLOBAL NAVIGATION SATELLITE SYSTE(全球卫星导航系统)”的缩写,作用类似于美国的GPS.欧洲的伽利略卫星定位系统.最早开发于苏联时期,后由俄罗斯继续该 ...

  10. Centos7.0关闭防火墙

    CentOS 7.0默认使用的是firewall作为防火墙,使用iptables必须重新设置一下 1.直接关闭防火墙 systemctl stop firewalld.service #关闭firew ...