ID | Title | Difficulty | |
---|---|---|---|
Loading... |
233. Number of Digit One
Hard
LeetCode
Math, Dynamic Programming, Recursion
Problem
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n.
Example 1:
Input: n = 13
Output: 6
Example 2:
Input: n = 0
Output: 0
Code
class Solution {
public int countDigitOne(int n) {
int res = 0;
for (long k = 1; k <= n; k *= 10) {
long r = n / k;
long m = n % k;
res += (r + 8) / 10 * k + (r % 10 == 1 ? m + 1 : 0);
}
return (int)res;
}
}
按 <- 键看上一题!
232. Implement Queue using Stacks
按 -> 键看下一题!
234. Palindrome Linked List