Number of Digit One(数学找规律)

来源:互联网 发布:java api 1.8中文在线 编辑:程序博客网 时间:2024/06/05 03:51

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

0 0