原题链接在这里:https://leetcode.com/problems/judge-route-circle/description/

题目:

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

题解:

判断'U'和'D' && 'R'和 'L'的个数是否相同.

Time Complexity: O(moves.length()). Space: O(1).

AC Java:

 class Solution {
public boolean judgeCircle(String moves) {
int x = 0;
int y = 0;
for(int i = 0; i<moves.length(); i++){
char c = moves.charAt(i);
if(c == 'U'){
y++;
}else if(c == 'D'){
y--;
}else if(c == 'R'){
x++;
}else if(c == 'L'){
x--;
}
}
return x==0 && y==0;
}
}

LeetCode Judge Route Circle的更多相关文章

  1. [LeetCode] Judge Route Circle 判断路线绕圈

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

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

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

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

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

  4. 657. Judge Route Circle【easy】

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

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

  6. Judge Route Circle

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

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

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

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

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

  9. 657. Judge Route Circle

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

随机推荐

  1. Cookie用法简介

    java操作Cookie---javax.servlet.http.Cookie 1.增加一个Cookie Cookie cookie = new Cookie("username" ...

  2. 【HackerRank】 Game Of Thrones - I

    King Robert has 7 kingdoms under his rule. He gets to know from a raven that the Dothraki are going ...

  3. Android摄像头测量尺(Advanced Ruler Pro)使用方法

    http://www.cnblogs.com/sinojelly/archive/2010/08/13/1799341.html Advanced Ruler Pro是一个Android手机应用程序, ...

  4. MongoDB命令语法小用

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using MongoDB; ...

  5. 20145240 《Java程序设计》第五周学习总结

    20145240 <Java程序设计>第五周学习总结 教材学习内容总结 语法与继承结构 8.1.1使用try.catch java中所有的错误都会被打包为对象,并提供了特有的语句进行处理. ...

  6. mongodb的原子性(Atomicity)和事物 (Transactions)

    在mongodb中,单个的写操作保持原子性是在单个的document 上. $isolated operator $isolated 一个写操作多个documents 的时候可以防止和其他进程交织,一 ...

  7. Python 时间日历类型

    # 时间日历 # time模块 # 提供了处理时间和表示之间转换的功能 # 获取当前时间戳 # 概念 # 从0时区的1970年1月1日0时0分0秒, 到所给定日期时间的秒数 # 浮点数 # 获取方式 ...

  8. pom.xml里使用了一系列的版本的框架,配置一个版本属性,让使用版本的都引用这个属性

    在pom.xml定义properties标签 <properties> <project.build.sourceEncoding>UTF-8</project.buil ...

  9. 安装使用snmp监控命令

    1.安装snmp windows下载安装https://sourceforge.net/projects/net-snmp/ centos yum install -y net-snmp net-sn ...

  10. vue-router scrollBehavior无效的问题及解决方案

    在使用vue做单页面应用开发时候 使用vue-router作为路由控制器  在使用过程中发现每个页面打开都在原来的位置 不能返回到页面顶部位置 ,然后查看api文档 滚动行为  发现如下代码: con ...