Title Description
Given an integer n, is less than the calculated number of all non-negative integer equal to n the number 1 appears.
输入: 13
输出: 6
解释: 数字 1 出现在以下数字中: 1, 10, 11, 12, 13 。
Reference Code
"Prove safety offer" 43 questions, see the book solution.
From the "digital laws" to proceed, step by step to find out the law through specific examples.
C ++ language version
class Solution {
public:
int countDigitOne(int n){
if(n <= 0)
return 0;
string str = to_string(n);
return mySolutionRecursively(str);
}
int mySolutionRecursively(string str){ // eg: 21345
int length = str.size();
if(length == 1){
if(str[0] == '0')
return 0;
else
return 1;
}
int firstNum = str[0] - '0';
int amountOfFirstDigit = 0;
if(firstNum == 1) // 当firstNum == 0时,此时amountOfFirstDigit == 0
amountOfFirstDigit = stoi(str.substr(1)) + 1;
else if(firstNum > 1)
amountOfFirstDigit = (int)pow(10, length-1);
int amountOfOtherDigit = 0;
amountOfOtherDigit = firstNum * (length-1) * (int)pow(10, length-2);
return amountOfFirstDigit + amountOfOtherDigit + mySolutionRecursively(str.substr(1));
}
};
C language version
class Solution {
public:
int countDigitOne(int n)
{
if(n <= 0)
return 0;
char strN[50];
sprintf(strN, "%d", n);
return mySolutionRecursively(strN);
}
int mySolutionRecursively(char *str){
int length = strlen(str);
if(length == 1){
if(str[0] == '0')
return 0;
else
return 1;
}
int firstNum = str[0] - '0';
int amountOfFirstDigit = 0;
if(firstNum == 1)
amountOfFirstDigit = (int)atoi(str+1) + 1;
else if(firstNum > 1)
amountOfFirstDigit = (int)pow(10, length-1);
int amountOfOtherDigit = 0;
amountOfOtherDigit = firstNum * (length-1) * (int)pow(10, length-2);
return amountOfFirstDigit + amountOfOtherDigit + mySolutionRecursively(str+1);
}
};