forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create 1088.Confusing-Number-II_v2.cpp
- Loading branch information
1 parent
1a93cfa
commit e10b3fd
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
Recursion/1088.Confusing-Number-II/1088.Confusing-Number-II_v2.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
class Solution { | ||
public: | ||
unordered_map<char,char>Map={{'0','0'},{'1','1'},{'6','9'},{'8','8'},{'9','6'}}; | ||
int count = 0; | ||
int N; | ||
string n; | ||
|
||
long perm(int l) | ||
{ | ||
return 4*pow(5,l-1); | ||
} | ||
|
||
long symmetric(int l) | ||
{ | ||
if (l%2==0) | ||
return 4*pow(5,l/2-1); | ||
else | ||
return 4*pow(5,l/2-1)*3; | ||
} | ||
|
||
int confusingNumberII(int N) | ||
{ | ||
this->N = N; | ||
n = to_string(N); | ||
|
||
for (int l=1; l<n.size(); l++) | ||
{ | ||
count += perm(l) - symmetric(l); | ||
} | ||
|
||
for (long x: {1,6,8,9}) | ||
dfs(x,1); | ||
return count; | ||
} | ||
|
||
void dfs(long num, int k) | ||
{ | ||
if (k==n.size()) | ||
{ | ||
if (num>N) return; | ||
if (isConfusing(num)) count++; | ||
return; | ||
} | ||
|
||
for (auto x:{0,1,6,8,9}) | ||
dfs(num*10+x, k+1); | ||
} | ||
|
||
bool isConfusing(int num) | ||
{ | ||
string s = to_string(num); | ||
int i=0; | ||
int j=s.size()-1; | ||
while (i<=j) | ||
{ | ||
if (Map[s[i]]!=s[j]) | ||
return true; | ||
i++; | ||
j--; | ||
} | ||
return false; | ||
} | ||
}; |