题解 | #整数中1出现的次数)#
整数中1出现的次数(从1到n整数中1出现的次数)
https://www.nowcoder.com/practice/bd7f978302044eee894445e244c7eee6
不用数位dp,纯计算的最简代码
public class Solution { public int NumberOf1Between1AndN_Solution(int n) { int ret = 0; for (long m = 1; m <= n; m *= 10) { long a = n / m, b = n % m; ret += (a + 8) / 10 * m + (a % 10 == 1 ? b + 1: 0); } return ret; } }