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
  • 相关阅读:
    RocketMQ同一个消费者唯一Topic多个tag踩坑经历
    1.写一个字符串反转函数.
    1.什么是灰度发布?
    简单比较 @EnableEurekaClient 和 @EnableDiscoveryClient 两个注解
    字符串拼接出现null的问题
    webSocket无法注入bean问题解决方案
    数据库中的时间类型需要指定长度吗?
    SQL语句的优化
    NoClassDefFoundError
    1.代码规范之 if 语句编写
  • 原文地址:https://www.cnblogs.com/immiao0319/p/8598965.html
Copyright © 2011-2022 走看看