https://leetcode.com/contest/weekly-contest-73/problems/rotated-digits/

slow:
class Solution {
public:
    int rotatedDigits(int N) {
        int total = 0;
        for (int i = 1; i <= N; i++) {
            int digit;
            int r = 0;
            int base = 1;
            int now = i;
            while (now) {
                digit = now % 10;
                now /= 10;
                if (digit == 0 || digit == 1 || digit == 8)
                    r += (digit * base);
                else if (digit == 2)
                    r += 5 * base;
                else if (digit == 5)
                    r += 2 * base;
                else if (digit == 6)
                    r += 9 * base;
                else if (digit == 9)
                    r += 6 * base;
                else {
                    r = i;
                    break;
                }
                base *= 10;
            }
            
            if (r != i) {
                //printf("* %d %d\n", r, i);
                total++;
            }
        }
        printf("total = %d\n", total);
        return total;
    }
};
fast:

留言

這個網誌中的熱門文章

https://leetcode.com/contest/leetcode-weekly-contest-52/problems/longest-univalue-path/

https://leetcode.com/contest/leetcode-weekly-contest-52/problems/maximum-sum-of-3-non-overlapping-subarrays/