NowCoder (opens new window)
public int NumberOf1Between1AndN_Solution(int n) { int cnt = 0; for (int m = 1; m <= n; m *= 10) { int a = n / m, b = n % m; cnt += (a + 8) / 10 * m + (a % 10 == 1 ? b + 1 : 0); } return cnt; }
Leetcode : 233. Number of Digit One (opens new window)
← 连续子数组的最大和 数字序列中的某一位数字→