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

[LeetCode]Number of Digit One,解题报告

时间:2015-07-30 13:33:17      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:leetcode

题目

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.


解题思路

题目理解

题目的意思是给定一个数n,计算[0, n]的区间中1出现的次数。

具体思路

在面试过程中如果被问到类似的题目,一定不要着急,因为这肯定是一道找规律的题目。最好的办法是在白纸上写一个差不多大的数,然后分析如何计算每一位1出现的次数。

以24071为例,如果我们想计算百位上1出现的次数,具体思路如下:

  1. 获取百位的高位为24,获取百位的低位为71,当前百位数字为0。
  2. 从高位出发来分析,百位出现的次数有:00100~00190,01100~01190,02100~02190,…,23100~23190。总共有(24*100)个1出现。

这是百位为0的情况,如果百位为1的时候,那除了上述的(24*100)个1之外,还包括:24100~24171,共(71 + 1)个1,即总共:24 * 100 + 71 + 1。

如果百位大于1例如为6的时候,那除了最开始的(24*100)个1之外,还包括了:24100~24199,共100个1,即总共:(24 + 1)*100。

总结规律

其实通过上面的分析,我们就可以归纳出一个规律如下。

要求第i位1出现的次数,并且i的高位数为highN,低位数为lowN,当前第i位的数字为cdigit,则当前i位1出现的次数分三种情况:

  1. cdigit == 0, count = highN * factor.
  2. cdigit == 1, count = highN * factor + lowN + 1.
  3. cdigit > 1, count = (highN + 1) * factor.

其中,factor为当前的乘积因子,例如百位的factor为100,十位的乘积因子为10。

有了规律,代码只要按照规律写出来即可。


AC代码

public class NumberofDigitOne {
    public int countDigitOne(int n) {
        int count = 0;
        long factor = 1;
        long cdigit = 0;
        long highN = 0;
        int lowN = 0;

        while (n / factor > 0) {
            cdigit = (n % (factor * 10)) / factor;
            highN = n / (factor * 10);

            if (cdigit == 0) {
                count += highN * factor;
            } else if (cdigit == 1) {
                count += highN * factor + lowN + 1;
            } else {
                count += (highN + 1) * factor;
            }

            lowN += cdigit * factor;
            factor *= 10;
        }


        return count;
    }

    public static void main(String[] args) {
        int n = 1410065408;
        NumberofDigitOne ndo = new NumberofDigitOne();
        int count = ndo.countDigitOne(n);

        System.out.println(count);
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

[LeetCode]Number of Digit One,解题报告

标签:leetcode

原文地址:http://blog.csdn.net/wzy_1988/article/details/47148263

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