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.

For example:
Given n = 13,

Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13.

public class Solution {
    public int countDigitOne(int n) {
        int ones = 0;
        for (long m = 1; m <= n; m *= 10)
            ones += (n/m + 8) / 10 * m + (n/m % 10 == 1 ? n%m + 1 : 0);
        return ones;
    }
}

http://www.cnblogs.com/aniy/articles/4676538.html

http://blog.csdn.net/xudli/article/details/46798619

http://blog.csdn.net/wangyunyun00/article/details/47342983

https://leetcode.com/discuss/44281/4-lines-o-log-n-c-java-python

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章