# Hard-题目26：233. Number of Digit One

Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n.

For example:
Given n = 13,
Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13.

public class Solution {
public int countDigitOne(int n) {
int[] powers = new int[]{0,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000,2147483647};
int k=0;
if(n==2147483647)
k=10;
else {
while(powers[k]<=n)
k++;
}

if(n<=0)
return 0;
else if(n>=1 && n<10)
return 1;
else {
k--;
return countDigitOne(powers[k]-1)*(n/powers[k])+countDigitOne(n%powers[k])+(n>=2*powers[k]?powers[k]:n-powers[k]+1);
}
}

}

0ms,7.97%,0ms,92.03%
cmershen的碎碎念：

• 本文已收录于以下专栏：

## LeetCode-233.Number of Digit One

https://leetcode.com/problems/number-of-digit-one/ Given an integer n, count the total number o...

## LeetCode 233. Number of Digit One

• jmspan
• 2016年04月06日 04:10
• 260

## leetcode 233: Number of Digit One

Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th...

## LeetCode No.233 Number of Digit One

LeetCode No.233 Number of Digit One

## 【LeetCode-Hard-2】【Number of Digit One】【1~N中‘1’出现次数】

• hjy0125
• 2015年07月21日 22:37
• 335

举报原因： 您举报文章：Hard-题目26：233. Number of Digit One 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)