leet-code/src/main/java/leetcode/editor/cn/NumberOfDigitOne.java

58 lines
1.2 KiB
Java
Raw Normal View History

2021-08-13 14:37:42 +08:00
//给定一个整数 n计算所有小于等于 n 的非负整数中数字 1 出现的个数。
//
//
//
// 示例 1
//
//
//输入n = 13
//输出6
//
//
// 示例 2
//
//
//输入n = 0
//输出0
//
//
//
//
// 提示:
//
//
// 0 <= n <= 2 * 109
//
// Related Topics 递归 数学 动态规划
// 👍 278 👎 0
package leetcode.editor.cn;
//233:数字 1 的个数
class NumberOfDigitOne {
public static void main(String[] args) {
//测试代码
Solution solution = new NumberOfDigitOne().new Solution();
//4
System.out.println(solution.countDigitOne(11));
//6
System.out.println(solution.countDigitOne(13));
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int countDigitOne(int n) {
int index = 1;
int count = 0;
while (index<=n){
count += (n / (index * 10)) * index + Math.min(Math.max(n % (index * 10) - index + 1, 0), index);
index *= 10;
}
return count;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}