leetcode (Rotated Digits)

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

Title:Rotated Digits   788

Difficulty:Easy

原题leetcode地址: https://leetcode.com/problems/rotated-digits/

1.   见代码

时间复杂度:O(n),一次一层for循环,虽然有嵌套循环。

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

    /**
     * 遍历累加
     * @param N
     * @return
     */
    public static int rotatedDigits(int N) {

        int result = 0;

        for (int i = 0; i <= N; i++) {
            if (isRotatedDigits(i)) {
                result++;
            }
        }

        return result;

    }

    /**
     * 2、5、6、9标记为true
     * @param num
     * @return
     */
    private static boolean isRotatedDigits(int num) {

        String s = num + "";
        boolean flag = false;

        for (int i = 0; i < s.length(); i++) {
            switch (s.charAt(i)) {
                case '2':
                case '5':
                case '6':
                case '9':
                    flag = true;
                    break;
                case '3':
                case '4':
                case '7':
                    return false;
            }
        }

        return flag;

    }

猜你喜欢

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