原题链接在这里: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. yii 2 局部关闭 CSRF 拦截

    最近在拿 yii 2.0 开发ajax提交,在 post 请求接口时,提示数据无法验证,于是查询 yii 错误日志,发现错误为 exception ‘yiiwebBadRequestHttpExcep ...

  2. UI控件之UICollectionView

    UICollectionView:集合视图,是iOS6.0后出现的,与UITableView类似,优势在于可以灵活的布局cell UICollectionViewLayout:布局类,抽象类,一般定义 ...

  3. JAVA ArraySet<E> SET形式的有序LIST

    Set形式的数组,数组内容重复 package com.sicdt.library.core.utils; import java.util.ArrayList; import java.util.C ...

  4. Qt5.3.0的安装与测试

    Qt5.3.0的安装与测试(交叉编译,用于arm,支持tslib触摸屏) 本次移植可以使用触摸屏. 首先下载源码包: http://download.qt.io/official_releases/q ...

  5. matplotlib模块之子图画法

    一般化的子图布局 首先要创建各个子图的坐标轴,传入一个四元列表参数:[x,y,width,height],用来表示这个子图坐标轴原点的x坐标.y坐标,以及宽和高.值得注意的是,这四个值的取值范围都是[ ...

  6. 如何在windows10环境下安装Pytorch-0.4.1版本

    开始是按照教程:https://blog.csdn.net/xiangxianghehe/article/details/80103095 安装了Pytorch0.4.0,但是安装后发现在import ...

  7. 关于C++ 中的this 的理解

    关键字this 通常被用在一个class内部,指正在被执行的该class的对象(object)在内存中的地址.它是一个指针,其值永远是自身object的地址.

  8. 简单web作业---书籍介绍的相关网页编写

    老师布置的web作业,我做了3个页面,其中有利用老师的css代码! 我有添加背景音乐,下面的是主界面的代码. <!DOCTYPE html> <html> <head&g ...

  9. Centos6.5下Hbase安装

    下载 http://mirror.bit.edu.cn/apache/hbase/hbase-0.94.26/hbase-0.94.26.tar.gz 2.  解压 tar -zxvf hbase-0 ...

  10. 提高MySQL效率与性能的技巧

    为查询缓存优化你的查询 大多数的MySQL服务器都开启了查询缓存.这是提高性最有效的方法之一,而且这是被MySQL的数据库引擎处理的.当有很多相同的查询被执行了多次的时候,这些查询结果会被放到一个缓存 ...