LeetCode--Judge Route Circle

思路:

    遍历字符串,如果'U'和'D'相等且'L'和'R'相等,则为true。

class Solution {
    public boolean judgeCircle(String moves) {
        int[] moves_arr=new int[]{0,0,0,0};
        for(int i=0;i<moves.length();i++){
            if(moves.charAt(i)=='U'){
                moves_arr[0]++;
            }
            if(moves.charAt(i)=='D'){
                moves_arr[1]++;
            }
            if(moves.charAt(i)=='L'){
                moves_arr[2]++;
            }
            if(moves.charAt(i)=='R'){
                moves_arr[3]++;
            }
        }
        
        if((moves_arr[0]==moves_arr[1]) && (moves_arr[2]==moves_arr[3])){
            return true;
        }
        else{
            return false;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_21752135/article/details/79979638