LeetCode刷题记录(第三天)

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.


翻译后:

最初,位置(0,0)处有一个机器人。给出它的一系列动作,判断这个机器人是否有一个圆圈,这意味着它移回到原来的位置

移动顺序由一个字符串表示。而每一个动作都是由一个人物来表现的。有效的机器人移动是R(右),L(左),U(上)和D(下)。输出应该是真或假,表示机器人是否做出圆圈。

自己解释:

就是一个有点傻的机器人,只会转圈走,但是有的时候会出故障,走了一圈没回去,所以要判断他走回去没有,如果走回去了,就说明他的系统没有问题,返回true,否则就是系统故障,返回false。但是我看来事例后才知道如何判断他走回去没有。

事例:

Example 1:

Input: "UD"
Output: true

Example 2:

Input: "LL"
Output: false

思路:1、定义四个变量,记录他四个方向走的距离;

         2、遍历传入的字符串,统计每个方向走出的距离;

         3、判断:如果向左走的距离等于像右走的距离,并且,向上走的距离等于向下走的距离,他就走回去了,否则就没回去。

package com.mianshi.suanfa.JudgeRouteCircle;

/**
 * Created by macbook_xu on 2018/3/21.
 */
public class Solution {
    public static boolean judgeCircle(String moves) {
        int l = 0;
        int r = 0;
        int u = 0;
        int d = 0;

        for (int i=0 ; i<moves.length() ; i++){
            String a = moves.charAt(i)+"";
            if(a.equals("L")){
                l++;
            }else if(a.equals("R")){
                r++;
            }else if(a.equals("U")){
                u++;
            }else if(a.equals("D")){
                d++;
            }else{
                return false;
            }
        }

        if(l==r && u==d){
            return true;
        }else{
            return false;
        }
    }

    public static void main(String[] args) {
        System.out.println(judgeCircle("UD"));
    }
}


虽然最后得出了结果,但是感觉代码还是比较冗余,而且用遍历对性能的消耗很大,所以还是看了大神们的编码。

public boolean judgeCircle(String moves) {
        moves=" " + moves + " ";
        return moves.split("L").length==moves.split("R").length && moves.split("U").length == moves.split("D").length;
    }

这位大神使用了java字符串中的一个函数:split(),这个函数是通过给定字符把一个字符串分割,把分割后的元素放到字符串数组中返回,这样,如果出现次数相同的元素,分割出的字符串数组的长度必然是相同的,直接比较它的长度即可。而且为什么要前后加两个空格呢?一开始我也不理解,但是当运行后发现,不加空格的话首尾两个元素则不会分割出字串,则不会计算他们的长度,影响了结果。所以得出结论:大神就是大神,小白就是小白。。。。。两行代码轻松解决。


当然,每个人都有不同的想法,所以还有很多不同的代码,虽然现在每天只做一道题,但是通过思考问题和学习大神们的代码,真的能学到很多新的想法与自己不经常使用的方法,下面再放几个不同版本的代码,今天就学这么多了。


public boolean judgeCircle(String moves) {
        int x = 0, y = 0;
        for(char c: moves.toCharArray()){
            x += (c=='R'?1:0) + (c=='L'?-1:0); y += (c=='U'?1:0) + (c=='D'?-1:0);
        }
        return x == 0 && y == 0;
    }

public class Solution {
    public boolean judgeCircle(String moves) {
        int x = 0;
        int y = 0;
        for (char ch : moves.toCharArray()) {
            if (ch == 'U') y++;
            else if (ch == 'D') y--;
            else if (ch == 'R') x++;
            else if (ch == 'L') x--;
        }
        return x == 0 && y == 0;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值