Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- void fun (long long number , long long r, vector <int> &vec, int &N, int &res, long long factor)
- {
- if (number > N)
- return;
- if (r != number)
- res++;
- for (int i = 0 ; i < vec.size(); i++)
- {
- if (vec[i] == -1 )
- continue;
- fun (number * 10 + i, vec[i] * factor + r , vec, N , res, factor * 10 );
- }
- }
- int confusingNumberII(int N) {
- vector <int> vec = {0,1,-1,-1,-1,-1,9,-1,8,6};
- int res = 0;
- fun (1,1,vec,N,res,10);
- fun (6,9,vec,N,res,10);
- fun (8,8,vec,N,res,10);
- fun (9,6,vec,N,res,10);
- return res;
- }
- };
- The function "backtrack" generates all possible numbers
- smaller than N and with only 0, 1, 6,8 and 9.
- When we have generated a new number,
- we check if the number is equal to the
- rotated version of it.
- If not, we increase the counter by 1.
- The function takes in
- the new number generated,
- the rotated version of it,
- and the "digit" for us to generate
- our next possible number.
- Since we want to avoid duplications here,
- we should not have 0 as our leading integer
- for any candidates - "00008"
- would be the same as
- "8" and if we put 0 at first, we will count 8 twice,
- which is not desired. Therefore,
- we run the helper function for 1, 6, 8, 9
- and return the total
- count value
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement