1. 程式人生 > >leetcode (Rotated Digits)

leetcode (Rotated Digits)

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;

    }