1 Star 1 Fork 0

laodasbch/Leetcode-Complete-Guide

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
902.txt 1.15 KB
一键复制 编辑 原始数据 按行查看 历史
JunB(66哥) 提交于 5年前 . Create 902.txt
class Solution {
String s;
int dp[][];
public int atMostNGivenDigitSet(String[] A, int n) {
s=n+"";
int res=0;
for(int i=1;i<s.length();i++){
int pro=1;
for(int j=0;j<i;j++){
pro*=A.length;
}
res+=pro;
}
dp=new int[s.length()][2];
for(int i=0;i<dp.length;i++){
Arrays.fill(dp[i],-1);
}
int cnt=dfs(A,0,1);
return res+cnt;
}
//only for length N
public int dfs(String A[],int index,int state){//0:same prefix 1:different
if(index>=s.length())return 1;
if(dp[index][state]!=-1)return dp[index][state];
int res=0;
for(int i=0;i<A.length;i++){
int d=Integer.parseInt(A[i]);
if(state==1){
if(d<(s.charAt(index)-'0')){
res+=dfs(A,index+1,0);
}
if(d==(s.charAt(index)-'0')){
res+=dfs(A,index+1,1);
}
}else{
res+=dfs(A,index+1,state);
}
}
dp[index][state]=res;
return res;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/laodasbch/Leetcode-Complete-Guide.git
git@gitee.com:laodasbch/Leetcode-Complete-Guide.git
laodasbch
Leetcode-Complete-Guide
Leetcode-Complete-Guide
master

搜索帮助