leet657机器人能否返回原点

两盒软妹~` 提交于 2019-12-09 12:55:01

在这里插入图片描述
官方给的题解

//7ms
   public static boolean judgeCircle(String moves) {
    int x = 0, y = 0;
        for(char move:moves.toCharArray()) {
        if (move == 'U') y--;
        else if (move == 'D') y++;
        else if (move == 'L') x--;
        else if (move == 'R') x++;
    }
        return x ==0&&y ==0;
}

我根据官方的改的我的题解

//12ms
public static boolean judgeCircle1(String moves) {
    int x = 0;
    int y = 0;
    for (int i = 0; i < moves.length(); i++) {
        if (moves.charAt(i) == 'L')
            x++;
        else if (moves.charAt(i) == 'R')
            y++;
        else if (moves.charAt(i) == 'U')
            x--;
        else if (moves.charAt(i) == 'D')
            y++;
    }

    return x == 0 && y == 0;
}

我自己写的

//18ms

public static boolean judgeCircle(String moves) {
    int Lcount =0;
    int Rcount =0;
    int Ucount =0;
    int Dcount =0;
    for (int i = 0; i <moves.length() ; i++) {
        if(moves.charAt(i)=='L')
            Lcount++;
        if(moves.charAt(i)=='R')
            Rcount++;
        if(moves.charAt(i)=='U')
            Ucount++;
        if(moves.charAt(i)=='D')
            Dcount++;
    }
    if(Math.abs(Lcount - Rcount)!=0)
        return false;
    if (Math.abs(Ucount - Dcount)!=0)
        return false;
    return true;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!