zoukankan      html  css  js  c++  java
  • 657. Judge Route Circle机器人能否返回

    [抄题]:

    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);
        }
    }
    View Code
  • 相关阅读:
    PHP查询手机号归属地
    MSVC版本号对应关系
    IT界名人录
    QT5 Win7 项目封装发行版(C++)
    ANSI编码
    微信小程序 for循环说明
    virtualbox 虚拟机 安装win7系统 无法联网
    go 实现两个goroutine交替执行打印数字和字母
    go 学习之反射
    go 学习之数据类型
  • 原文地址:https://www.cnblogs.com/immiao0319/p/8598965.html
Copyright © 2011-2022 走看看