贴一个C++版代码 class Solution { public: int NumberOf1Between1AndN_Solution(int n) { int base=1; int res=0; while(base<=n){ int cur=n/base%10; int a=n/base/10; int b=n%base; //分三种情况讨论 if(cur==1){ res+=a*base+b+1; }else if(cur==0){ res+=a*base; }else{ res+=(a+1)*base; } base*=10; } return res; } };
1

相关推荐

牛客网
牛客企业服务