力扣链接:https://leetcode-cn.com/problems/1nzheng-shu-zhong-1chu-xian-de-ci-shu-lcof/
题目描述
输入一个整数 n ,求1~n这n个整数的十进制表示中1出现的次数。
例如,输入12,1~12这些整数中包含1 的数字有1、10、11和12,1一共出现了5次。
思路:(数学规律)
题目要求我们统计n个整数中1出现的次数,我们反过来考虑,逐位统计当某为1时,可以产生多少个n以内的数。
由于有一位被固定为1,在统计个数时我们可以直接把这一位拿掉,将其前后两部分直接拼接起来,这样就成了计算连续的数字有多少个。
假设当前统计的位上的数字为cur,cur左边的数字组成的数为high,右边的为low,cur所在位为digit(表示10^digit位),总共有以下几种情况:
算法:
代码:
/** * @param {number} n * @return {number} */ var countDigitOne = function(n) { let digit = 1, high = Math.floor(n / 10), cur = n % 10, low = 0; let res = 0; while(!(high === 0 && cur === 0)){ if(cur === 0){ res += high * digit; }else if(cur === 1){ res += high * digit + low + 1; }else{ res += (high + 1) * digit; } low += cur * digit; cur = high % 10; high = Math.floor(high / 10); digit *= 10; } return res; };
时间复杂度:O(logN) (N的位数)
空间复杂度:O(1)
原文:https://www.cnblogs.com/xintangchn/p/13277149.html