[抄题]:

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

[暴力解法]:存count[4]数组中

时间分析:

空间分析:n

[优化后]:因为判断抵消效应,只用一个变量++--足矣

时间分析:

空间分析:1

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

转化成字符串数组后再操作

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

判断抵消效应只用一个变量就行了

[复杂度]:Time complexity: O(n) Space complexity: O(1)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

class Solution {
public boolean judgeCircle(String moves) {
//cc
if (moves == null) {
return false;
}
//array
int x = 0, y = 0;
for (char c : moves.toCharArray()) {
if (c == 'R') {
x++;
}
if (c == 'L') {
x--;
}
if (c == 'U') {
y++;
}
if (c == 'D') {
y--;
}
}
//return x && y
return (x == 0 && y == 0);
}
}

657. Judge Route Circle机器人能否返回的更多相关文章

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

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

  2. 657. Judge Route Circle【easy】

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

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

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

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

  5. 657. Judge Route Circle

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

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

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

  9. LeetCode Judge Route Circle

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

随机推荐

  1. 使用cookie纪录访问次数

    由于是简单的demo,我就没有链接数据库,退出重新登陆访问次数清零,只能靠下刷新来维持下访问次数 把用户名和次数初始化放进cookie Cookie uesrnameCookie = new Cook ...

  2. 【备忘录】CentOS服务器mysql忘记root密码恢复

    mysql的root忘记,现无法操作数据库 停止mysql服务service  mysql stop 然后使用如下的参数启动mysql, --skip-grant-tables会跳过mysql的授权 ...

  3. Android UI一些技巧

    (1)去掉EditText的背景颜色  android:background="@null" (2)ListView 修改某行的值,因为一些功能逻辑,需要修改ListView某行的 ...

  4. 杂项-数学软件:Mathematica

    ylbtech-杂项-数学软件:Mathematica Mathematica是一款科学计算软件,很好地结合了数值和符号计算引擎.图形系统.编程语言.文本系统.和与其他应用程序的高级连接.很多功能在相 ...

  5. python学习(二十) Python 中的比较:is 与 ==

    Python 中的比较:is 与 == 在 Python 中会用到对象之间比较,可以用 ==,也可以用 is .但是它们的区别是什么呢? is 比较的是两个实例对象是不是完全相同,它们是不是同一个对象 ...

  6. node的express中间件之bodyParser

    bodyParser用于解析客户端请求的body中的内容,内部使用JSON编码处理,url编码处理以及对于文件的上传处理. 下面是一个文件上传的例子. 建立一个1.html页面 <!DOCTYP ...

  7. PHP for和foreach的区别

    首先,我们先准备两个用于遍历的数组: $arr1=array(1=>'a', 3=>22, 5=>'b', 4=>'c', 8=>'d'); $arr2=array('a ...

  8. JeeSite导出多条数据(加复选框)demo

    表格图: jsp: 后台: @RequiresPermissions("shwindow:advertisementPutInList:view") @RequestMapping ...

  9. ad采样后幅度的衰减

    adc采集到的信号对低频有一定的衰减.因为要确定衰减的程度.通过da输出到示波器上观察. 数据如下: 输入 输出(enable) 输出(disable) 1v(20hz) 1v 0.88v 1v(10 ...

  10. Java string String

    java.lang.String string这个不是关键字 关String的讲解,参看:http://www.cnblogs.com/octobershiner/archive/2012/04/02 ...