-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountDigitOne.h
59 lines (56 loc) · 1.47 KB
/
countDigitOne.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//
// Created by so_go on 2020/1/13.
//
/*
* 给定一个整数 n,计算所有小于等于 n 的非负整数中数字 1 出现的个数。
示例:
输入: 13
输出: 6
解释: 数字 1 出现在以下数字中: 1, 10, 11, 12, 13 。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/number-of-digit-one
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
*/
#ifndef SRC_COUNTDIGITONE_H
#define SRC_COUNTDIGITONE_H
#include<cmath>
using namespace std;
class CountDigitOne {
public:
/*
*
理解错误,理解成出现1的数字的个数
int f(int k){
int res = 0;
for(int i = 0; i <= k - 1; i++){
int tmp = pow(10, i) * pow(9, k - 1 - i);
res += tmp;
}
return res;
}
int countDigitOne(int n) {
int digit, newn = n, ws = 0;
while(newn > 0){
digit = newn % 10;
ws += 1;
newn = newn / 10;
}
cout << n << ' ' << digit << ' ' << ws << endl;
int curNum;
if(n == 0){
return 0;
}
if(ws == 1){
return 1;
}
if( ws > 1 and digit == 1){
return f(ws - 1) + n - digit * pow(10, ws - 1) + 1;
}
else{
curNum = pow(10, ws - 1) + (digit - 1) * f( ws - 1);
return curNum + countDigitOne(n - digit * pow(10, ws - 1));
}
}
*/
};
#endif //SRC_COUNTDIGITONE_H