程式語言 - LeetCode - C++ - 788. Rotated Digits



題目:


解答:

class Solution {
public:
    int rotatedDigits(int n) {
        auto is_good = [](int x) {
            bool ans = false;

            while (x > 0) {
                int d = x % 10;

                if (d == 3 || d == 4 || d == 7) {
                    return false;
                }

                if (d == 2 || d == 5 || d == 6 || d == 9) {
                    ans = true;
                }

                x /= 10;
            }

            return ans;
        };

        int ans = 0;

        for (int i = 1; i <= n; ++i) {
            if (is_good(i) == true) {
                ans += 1;
            }
        }

        return ans;
    }
};