码迷,mamicode.com
首页 > 其他好文 > 详细

#Leetcode# 233. Number of Digit One

时间:2019-04-13 10:34:55      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:mil   family   int   amp   highlight   pre   follow   col   etc   

https://leetcode.com/problems/number-of-digit-one/

 

Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n.

Example:

Input: 13
Output: 6 
Explanation: Digit 1 occurred in the following numbers: 1, 10, 11, 12, 13.

代码:

class Solution {
public:
    int countDigitOne(int n) {
        int res = 0;
        long long a = 1, b = 1;
        while (n) {
            res += (n + 8) / 10 * a + (n % 10 == 1) * b;
            b += n % 10 * a;
            a *= 10;
            n /= 10;
        }
        return res;
    }
};

  暴力超时 新的一天从被数论教做人开始

#Leetcode# 233. Number of Digit One

标签:mil   family   int   amp   highlight   pre   follow   col   etc   

原文地址:https://www.cnblogs.com/zlrrrr/p/10699816.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!