leetcode:数学:Number of Digit One(233)

原创 2016年08月30日 15:46:24

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

For example:
Given n = 13,
Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13.


class Solution {
public:
    int countDigitOne(int n) {
        int res = 0, a = 1, b = 1;
        while (n > 0) {
            res += (n + 8) / 10 * a + (n % 10 == 1) * b;
            b += n % 10 * a;
            a *= 10;
            n /= 10;
        }
        return res;
    }
};
版权声明:本文为博主原创文章,未经博主允许不得转载。

LeetCode No.233 Number of Digit One

LeetCode No.233 Number of Digit One

[LeetCode]233.Number of Digit One

题目Given an integer n, count the total number of digit 1 appearing in all non-negative integers less ...

LeetCode Algorithms 233. Number of Digit One 题解

题目:Given an integer n, count the total number of digit 1 appearing in all non-negative integers less...

算法Week02.04 - LeetCode 233. Number of Digit One

算法Week02.04 - LeetCode 233. Number of Digit One

【LeetCode-233】Number of Digit One

一看到这道题就想到了动态规划,但是tle了,先看看我的python代码吧! class Solution(object): # 最容易想到的方法了(动态规划),但是tle了 def ...

LeetCode 233. Number of Digit One

原题网址:https://leetcode.com/problems/number-of-digit-one/ Given an integer n, count the total number...
  • jmspan
  • jmspan
  • 2016年04月06日 04:10
  • 260

leetcode 233: Number of Digit One

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

LeetCode-233.Number of Digit One

https://leetcode.com/problems/number-of-digit-one/ Given an integer n, count the total number o...

leetcode -- Number of Digit One -- 数学题,找规律

https://leetcode.com/problems/number-of-digit-one/参考http://www.cnblogs.com/grandyang/p/4629032.htmlh...
  • xyqzki
  • xyqzki
  • 2015年12月16日 17:11
  • 268

剑指offer 面试题32:从1到n整数中1出现的次数(leecode233. Number of Digit One) 题解

剑指offer 面试题32:从1到n整数中1出现的次数(Leecode233. Number of Digit One) 提交网址: http://www.nowcoder.com/prac...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode:数学:Number of Digit One(233)
举报原因:
原因补充:

(最多只允许输入30个字)