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

    题目大概的意思是有个机器人,给定一些列动作判断它最后回到原点没。
    思路很简单,回到原点换个话说就是R和L操作数量一样,U和D操作数一样即可。
    public class judgeCircle {
        public static boolean judgeCircle(String moves) {
            if(moves!=null&&moves.length()>0)
            {
                 int ud=0;
                    int lr=0;
                    for(int i=0;i<moves.length();i++)
                    {
                        if(moves.charAt(i)=='U')
                        {
                            ud++;
                        }
                        if(moves.charAt(i)=='D')
                        {
                            ud--;
                        }
                        if(moves.charAt(i)=='L')
                        {
                            lr++;
                        }
                        if(moves.charAt(i)=='R')
                        {
                            lr--;
                        }
                    }
                    if(ud==0&&lr==0)
                    {
                        return true;
                    }
                    else {
                        return false;
                    }
            }
            else {
                return false;
            }
           
        }
        public static void main(String args[])
        {
            System.out.println(judgeCircle("UD"));
        }
    }
  • 相关阅读:
    Splay专题总结
    UVa12657
    ZOJ3772
    POJ1743
    高斯消元模板
    python使用chrom登陆微博
    mysql常用数据库(表)命令
    mysql索引
    mysql建表的时候,时间戳的选用
    php 金额每三位添加一个逗号
  • 原文地址:https://www.cnblogs.com/icysnow/p/8126704.html
Copyright © 2011-2022 走看看