Advertisement
nikunjsoni

1088

Jul 13th, 2021
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     vector<int> m = {0, 1, -1, -1, -1, -1, 9, -1, 8, 6};
  4.     vector<int> v = {0, 1, 6, 8, 9};
  5.     int ans = 0;
  6.     int confusingNumberII(int N) {
  7.         for(int n:{1,6,8,9})
  8.             dfs(n, N);
  9.         return ans;
  10.     }
  11.    
  12.     void dfs(int n, int limit){
  13.         if(n > limit) return;
  14.         if(isConfusing(n))
  15.             ans++;
  16.         if(n > 1e8) return;
  17.         for(int next:v)
  18.             dfs(n*10+next, limit);
  19.     }
  20.    
  21.     bool isConfusing(int n){
  22.         int num=0, tmp=n;
  23.         while(n){
  24.             int r=n%10;
  25.             n /= 10;
  26.             if(m[r] == -1) return false;
  27.             num = num*10+m[r];
  28.         }
  29.         return (num != tmp);
  30.     }
  31. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement