# 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.

class Solution {
public:
int countDigitOne(int n) {
if (n <= 0)
{
return 0;
}
int curr = n, right = 0, result = 0;
while (curr > 0)
{
if (curr%10 > 1)
{
result += (curr/10+1)*pow(10, right);
}
else if (curr%10 == 1)
{
result += (curr/10)*pow(10, right) + n%int(pow(10, right)) + 1;
}
else
{
result += (curr/10)*pow(10, right);
}
curr /= 10;
right++;
}
return result;
}
};

#### leetcode 233: Number of Digit One

2015-07-08 07:53:14

#### 233 - Number of Digit One

2015-08-07 18:03:22

#### [LeetCode]Number of Digit One,解题报告

2015-07-30 12:34:10

#### C++版 - 剑指offer 面试题32：从1到n整数中1出现的次数(leecode233. Number of Digit One) 题解

2016-05-04 00:47:45

#### LeetCode OJ 之 Number of Digit One （数字1的个数）

2015-08-24 15:19:45

#### 经典算法——Number of Digit One

2016-04-03 17:05:21

#### leetcode hard模式专杀之233. Number of Digit One

2017-07-18 11:28:49

#### 233. Number of Digit One【H】【33】【再来一遍】

2016-05-21 16:46:28

#### leetcode 233. 数字1的个数(Number of Digit One)

2018-05-06 11:51:00

#### 杭电ACM 1041 Computer Transformation java代码详解AC

2015-11-04 13:31:43