具体思想:
dfs就可以,但是坑在边界判定上,有点繁琐;
具体代码:
class Solution {
public:
int atMostNGivenDigitSet(vector<string>& digits, int n) {
int cnt=0;
string s="";
while(n!=0){
s.insert(s.begin(),'0'+n%10);
n/=10;
}
cnt+=shallow_cnt(digits.size(),s.size()-1);
cnt+=digit_cnt(digits,s,0);
return cnt;
}
int shallow_cnt(int m,int n){
int cnt=0;
while(n!=0){
cnt+=pow(m,n);
n--;
}
return cnt;
}
int digit_cnt(vector<string>& digits,string& s,int index){
if(index==s.size())
return 1;
int cnt=0;
int top_dight=s[index]-'0';
for(int i=0;i<digits.size();i++){
int num=digits[i][0]-'0';
if(num<top_dight){
cnt+=pow(digits.size(),s.size()-1-index);
}else if(num==top_dight){
int res=1;
res*=digit_cnt(digits, s, index+1);
cnt+=res;
}
}
return cnt;
}
};