Advertisement
SalmaYasser

Untitled

Jan 8th, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. class Solution {
  2. public:
  3.  
  4.  
  5. void fun (long long number , long long r, vector <int> &vec, int &N, int &res, long long factor)
  6. {
  7. if (number > N)
  8. return;
  9. if (r != number)
  10. res++;
  11.  
  12. for (int i = 0 ; i < vec.size(); i++)
  13. {
  14. if (vec[i] == -1 )
  15. continue;
  16.  
  17. fun (number * 10 + i, vec[i] * factor + r , vec, N , res, factor * 10 );
  18. }
  19.  
  20. }
  21. int confusingNumberII(int N) {
  22.  
  23. vector <int> vec = {0,1,-1,-1,-1,-1,9,-1,8,6};
  24.  
  25.  
  26. int res = 0;
  27. fun (1,1,vec,N,res,10);
  28. fun (6,9,vec,N,res,10);
  29. fun (8,8,vec,N,res,10);
  30. fun (9,6,vec,N,res,10);
  31. return res;
  32.  
  33. }
  34. };
  35. The function "backtrack" generates all possible numbers
  36. smaller than N and with only 0, 1, 6,8 and 9.
  37. When we have generated a new number,
  38. we check if the number is equal to the
  39. rotated version of it.
  40. If not, we increase the counter by 1.
  41. The function takes in
  42. the new number generated,
  43. the rotated version of it,
  44. and the "digit" for us to generate
  45. our next possible number.
  46. Since we want to avoid duplications here,
  47. we should not have 0 as our leading integer
  48. for any candidates - "00008"
  49. would be the same as
  50. "8" and if we put 0 at first, we will count 8 twice,
  51. which is not desired. Therefore,
  52. we run the helper function for 1, 6, 8, 9
  53. and return the total
  54. count value
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement