Rotated Digits (Easy Google)
Input: 10
Output: 4
Explanation:
There are four good numbers in the range `[1, 10] : 2, 5, 6, 9`.
Note that 1 and 10 are not good numbers, since they remain unchanged after rotating.Basic Idea:
class Solution { bool isGood(int num) { int _map[10] = {1, 1, 1, 0, 0, 1, 1, 0, 1, 1}; // 表示0125689可以翻转180度 bool contains2569 = false; while (num > 0) { int digit = num % 10; num /= 10; if (! _map[digit]) return false; if (digit == 2 || digit == 5 || digit == 6 || digit == 9) contains2569 = true; } return contains2569; } public: int rotatedDigits(int N) { int ret = 0; for (int i = 1; i <= N; ++i) { ret += isGood(i); } return ret; } };