剑指offer32 整数中1出现的次数(从1到n整数中1出现的次数)

class Solution {
public:
    int NumberOf1Between1AndN_Solution(int n)
    {
        if(n<=0)
            return 0;
        int count=0;
        int i=1;
        while(i<=n)
        {
            int p=i;
            while(p)
            {
                if(p%10==1)
                    count++;
                p=p/10;
            }
            i++;
        }
        return count;
    
    }
};

 

posted on 2016-05-28 21:54  summerkiki  阅读(97)  评论(0编辑  收藏  举报