程序员面试题精选100题(25)-在从1到n的正数中1出现的次数[算法]
題目:輸入一個(gè)整數(shù)n,求從1到n這n個(gè)整數(shù)的十進(jìn)制表示中1出現(xiàn)的次數(shù)。
例如輸入12,從1到12這些整數(shù)中包含1?的數(shù)字有1,10,11和12,1一共出現(xiàn)了5次。
分析:這是一道廣為流傳的google面試題。用最直觀的方法求解并不是很難,但遺憾的是效率不是很高;而要得出一個(gè)效率較高的算法,需要比較強(qiáng)的分析能力,并不是件很容易的事情。當(dāng)然,google的面試題中簡(jiǎn)單的也沒(méi)有幾道。
首先我們來(lái)看最直觀的方法,分別求得1到n中每個(gè)整數(shù)中1出現(xiàn)的次數(shù)。而求一個(gè)整數(shù)的十進(jìn)制表示中1出現(xiàn)的次數(shù),就和本面試題系列的第22題很相像了。我們每次判斷整數(shù)的個(gè)位數(shù)字是不是1。如果這個(gè)數(shù)字大于10,除以10之后再判斷個(gè)位數(shù)字是不是1。基于這個(gè)思路,不難寫(xiě)出如下的代碼:
int NumberOf1(unsigned int n);/ // Find the number of 1 in the integers between 1 and n // Input: n - an integer // Output: the number of 1 in the integers between 1 and n / int NumberOf1BeforeBetween1AndN_Solution1(unsigned int n) {int number = 0;// Find the number of 1 in each integer between 1 and nfor(unsigned int i = 1; i <= n; ++ i)number += NumberOf1(i);return number; }/ // Find the number of 1 in an integer with radix 10 // Input: n - an integer // Output: the number of 1 in n with radix / int NumberOf1(unsigned int n) {int number = 0;while(n){if(n % 10 == 1)number ++;n = n / 10;}return number; }
這個(gè)思路有一個(gè)非常明顯的缺點(diǎn)就是每個(gè)數(shù)字都要計(jì)算1在該數(shù)字中出現(xiàn)的次數(shù),因此時(shí)間復(fù)雜度是O(n)。當(dāng)輸入的n非常大的時(shí)候,需要大量的計(jì)算,運(yùn)算效率很低。我們?cè)囍页鲆恍┮?guī)律,來(lái)避免不必要的計(jì)算。
我們用一個(gè)稍微大一點(diǎn)的數(shù)字21345作為例子來(lái)分析。我們把從1到21345的所有數(shù)字分成兩段,即1-1235和1346-21345。
先來(lái)看1346-21345中1出現(xiàn)的次數(shù)。1的出現(xiàn)分為兩種情況:一種情況是1出現(xiàn)在最高位(萬(wàn)位)。從1到21345的數(shù)字中,1出現(xiàn)在10000-19999這10000個(gè)數(shù)字的萬(wàn)位中,一共出現(xiàn)了10000(104)次;另外一種情況是1出現(xiàn)在除了最高位之外的其他位中。例子中1346-21345,這20000個(gè)數(shù)字中后面四位中1出現(xiàn)的次數(shù)是2000次(2*103,其中2的第一位的數(shù)值,103是因?yàn)閿?shù)字的后四位數(shù)字其中一位為1,其余的三位數(shù)字可以在0到9這10個(gè)數(shù)字任意選擇,由排列組合可以得出總次數(shù)是2*103)。
至于從1到1345的所有數(shù)字中1出現(xiàn)的次數(shù),我們就可以用遞歸地求得了。這也是我們?yōu)槭裁匆?/span>1-21345分為1-1235和1346-21345兩段的原因。因?yàn)榘?/span>21345的最高位去掉就得到1345,便于我們采用遞歸的思路。
分析到這里還有一種特殊情況需要注意:前面我們舉例子是最高位是一個(gè)比1大的數(shù)字,此時(shí)最高位1出現(xiàn)的次數(shù)104(對(duì)五位數(shù)而言)。但如果最高位是1呢?比如輸入12345,從10000到12345這些數(shù)字中,1在萬(wàn)位出現(xiàn)的次數(shù)就不是104次,而是2346次了,也就是除去最高位數(shù)字之后剩下的數(shù)字再加上1。
基于前面的分析,我們可以寫(xiě)出以下的代碼。在參考代碼中,為了編程方便,我把數(shù)字轉(zhuǎn)換成字符串了。
#include "string.h" #include "stdlib.h"int NumberOf1(const char* strN); int PowerBase10(unsigned int n);/ // Find the number of 1 in an integer with radix 10 // Input: n - an integer // Output: the number of 1 in n with radix / int NumberOf1BeforeBetween1AndN_Solution2(int n) {if(n <= 0)return 0;// convert the integer into a stringchar strN[50];sprintf(strN, "%d", n);return NumberOf1(strN); }/ // Find the number of 1 in an integer with radix 10 // Input: strN - a string, which represents an integer // Output: the number of 1 in n with radix / int NumberOf1(const char* strN) {if(!strN || *strN < '0' || *strN > '9' || *strN == '\0')return 0;int firstDigit = *strN - '0';unsigned int length = static_cast<unsigned int>(strlen(strN));// the integer contains only one digitif(length == 1 && firstDigit == 0)return 0;if(length == 1 && firstDigit > 0)return 1;// suppose the integer is 21345// numFirstDigit is the number of 1 of 10000-19999 due to the first digitint numFirstDigit = 0;// numOtherDigits is the number of 1 01346-21345 due to all digits// except the first oneint numOtherDigits = firstDigit * (length - 1) * PowerBase10(length - 2);// numRecursive is the number of 1 of integer 1345int numRecursive = NumberOf1(strN + 1);// if the first digit is greater than 1, suppose in integer 21345// number of 1 due to the first digit is 10^4. It's 10000-19999if(firstDigit > 1)numFirstDigit = PowerBase10(length - 1);// if the first digit equals to 1, suppose in integer 12345// number of 1 due to the first digit is 2346. It's 10000-12345else if(firstDigit == 1)numFirstDigit = atoi(strN + 1) + 1;return numFirstDigit + numOtherDigits + numRecursive; }/ // Calculate 10^n / int PowerBase10(unsigned int n) {int result = 1;for(unsigned int i = 0; i < n; ++ i)result *= 10;return result; }
本文已經(jīng)收錄到《劍指Offer——名企面試官精講典型編程題》一書(shū)中,有改動(dòng),書(shū)中的分析講解更加詳細(xì)。歡迎關(guān)注。
本題已被九度Online Judge系統(tǒng)收錄,歡迎讀者移步到http://ac.jobdu.com/hhtproblems.php在線測(cè)試自己的代碼。
博主何海濤對(duì)本博客文章享有版權(quán)。網(wǎng)絡(luò)轉(zhuǎn)載請(qǐng)注明出處http://zhedahht.blog.163.com/。整理出版物請(qǐng)和作者聯(lián)系。
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專(zhuān)家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的程序员面试题精选100题(25)-在从1到n的正数中1出现的次数[算法]的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 程序员面试题精选100题(24)-栈的p
- 下一篇: 程序员面试题精选100题(26)-和为n