233. Number of Digit One
来源:互联网 发布:python 爬虫 去标签 编辑:程序博客网 时间:2024/05/17 01:05
public class Solution { public int countDigitOne(int n) { if(n<=0)return 0; return dfs(n); } private int dfs(int n){ int length = size(n); if(length==1){ if(n==0)return 0; else{ return 1; } } if(length>1){ int count=0;//首位一的个数 int len = (n-(shengcheng(length-1)+1))/(shengcheng(length-1)+1);//shengcheng(length-1)+1,其实就是生成整跟n相同位数的整十数,如10,100,1000,10000000, int firstNum = n/(shengcheng(length-1)+1); int behindNum = n-firstNum*(shengcheng(length-1)+1); if (len>0)count=shengcheng(length-1)+1; else { count = n-(shengcheng(length-1)+1)+1; } return dfs(shengcheng(length-1))+len*dfs(shengcheng(length-1))+count+dfs(behindNum); }//比如321=dfs(99)+2*dfs(99)+100+dfs(21),dfs(1092)=dfs(999)+0*dfs(999)+92+dfs(92),那321来看最合适 return 0; } public int size(int n){//测量数的位数 int count = 1; while (n/10>0){ n = n/10; count++; } return count; } public int shengcheng(int n){//生成99.999.9999这样的数三位就是999,4位就是9999 int count=1; for(int i=0;i<n;i++) count = count*10; return count-1; }}
0 0
- 233. Number of Digit One
- 233. Number of Digit One
- 233. Number of Digit One
- 233. Number of Digit One
- 233. Number of Digit One
- 233. Number of Digit One
- 233. Number of Digit One
- Number of Digit One
- Number of Digit One
- Number of Digit One
- Number of Digit One
- Number of Digit One
- Number of Digit One
- Number of Digit One
- Number of Digit One
- Number of Digit One
- Number of Digit One
- Number of Digit One
- (UVA1395)Slim Span(A)
- 10.31 NOIP模拟赛 (afternoon)
- ssh整合(4)Spring与hibernate整合
- (译)LearnOpenGL实际案例Breakout(十):能量块
- 通过自带SSH框架的搭建过程注意和jar包解释
- 233. Number of Digit One
- android 自定义view实现表盘效果
- Linux getpid函数
- 二叉树遍历应用114. Flatten Binary Tree to Linked List
- android graphic(8)—surface申请GraphicBuffer过程
- scanf 和printf 的相关注意事项
- 自苦
- 关于RotateRect的一些说明
- 01 计算机常识