leetcode (Robot Return to Origin)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hsx1612727380/article/details/85110302

Title:Robot Return to Origin    657

Difficulty:Easy

原题leetcode地址:https://leetcode.com/problems/robot-return-to-origin/

1.    上下对比,左右对比

时间复杂度:O(n),一次一层for循环。

空间复杂度:O(1),没有申请额外空间。

    /**
     * 上线对比,左右对比
     * @param moves
     * @return
     */
    public static boolean judgeCircle(String moves) {

        int udCount = 0;
        int lrCount = 0;

        for (int i = 0; i < moves.length(); i++) {
            if (moves.charAt(i) == 'U') {
                udCount++;
            }
            if (moves.charAt(i) == 'D') {
                udCount--;
            }
            if (moves.charAt(i) == 'L') {
                lrCount++;
            }
            if (moves.charAt(i) == 'R') {
                lrCount--;
            }
        }

        if (udCount == 0 && lrCount == 0) {
            return true;
        }

        return false;

    }

猜你喜欢

转载自blog.csdn.net/hsx1612727380/article/details/85110302