# LeetCode-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) {
long long ans = 0, base = 1, last = 0;
while (n)
{
int t = n % 10;
n /= 10;
if (t > 1)ans += (n + 1) * base;
else if (t == 1)ans += n*base + last + 1;
else ans += n*base;
last = t*base + last;
base *= 10;

}
return ans;
}
};

#### LeetCode-Number of Digit One

2018-04-07 20:51:27

#### *LeetCode-Number of Digit One

2015-10-27 04:49:43

#### LeetCode-Number of 1 Bits-解题报告

2015-07-08 19:19:58

#### LeetCode-Number of Islands-解题报告

2015-07-08 19:13:51

#### encode_one_frame ()中几个主要函数说明

2014-10-24 15:03:31

#### LeetCode-Number of Digit One（编程之美-1的数目）

2015-08-29 18:05:33

#### oracle lz047中的REGEXP_LIKE(cust_first_name,'[[:digit:]]'))

2011-08-12 09:55:45

#### poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告

2009年10月06日 9KB 下载

#### oracle 10支持正则表达式REGEXP_LIKE(content,'[[:digit:]]'))

2013-10-24 09:15:43

#### 【Leetcode】400. Nth Digit

2016-09-18 13:58:43