样例例如
n=12,k=1,在 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],我们发现1出现了5次 (1, 10, 11, 12)
代码如下:
class Solution { public: /* * param k : As description. * param n : As description. * return: How many k's between 0 and n. */ int digitCounts(int k, int n) { // write your code here int i,j,num=0; if(k==0) num=1; for(i=0;i<=n;i++) { j=i; while(j!=0) { if(j%10==k) num++; j=j/10; } } return num; } };
由于是0的时候,0不能对0进行相除,所以num的初值为1.