题目:输入一个整数 n ,求1~n这n个整数的十进制表示中1出现的次数。
例如,输入12,1~12这些整数中包含1 的数字有1、10、11和12,1一共出现了5次。
示例1:
输入:n = 12
输出:5
示例2:
输入:n = 13
输出:6
思路:借鉴题解区一位大佬的思路:
class Solution {
public int countDigitOne(int n) {
return f(n);
}
public int f(int n){
if(n<=0) return 0;
String s = String.valueOf(n);
int high = s.charAt(0)-'0';
int pow = (int)Math.pow(10,s.length()-1);
int last = n- high*pow;
if(high == 1){
return f(pow-1)+last+1+f(last);
}else{
return high*f(pow-1)+f(last)+pow;
}
}
}