题解 | #整数中1出现的次数(从1到n整数中1出现的次数)#

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

http://www.nowcoder.com/practice/bd7f978302044eee894445e244c7eee6

    public int NumberOf1Between1AndN_Solution(int n) {
     int high = n/10;
     int cur = n%10;
     int d = 1;
     int low = 0;
        int count = 0;
        while(cur != 0 || high != 0){
            if(cur == 0){
                count = count + high * d;
            }else if(cur == 1){
                count = count + high * d + low + 1;
         }else{
                count = count + (high+1)*d;
            }
            low = low + cur*d;
            cur = high%10;
            high = high/10;
            d = d *10;
     }
        return count;
    }
}
全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务