Leetcode-number of Digit One (number of programming beauty-1)

Source: Internet
Author: User

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.

The problem is not difficult, but it is difficult to analyze, first of all we think of is the decomposition of the problem, find the law, the big problem small problem, and then in the programming solution.

It is obvious that there is a better way to do this is to traverse from the 1~n, each time to determine the number of each I 1, obviously relatively slow.

There must be another way to analyze it directly when given N. This time the analysis of the problem can start from the n=1, using inductive method, find out what the law.

Obviously, we can follow the one-digit, that is, in the single-digit, 10-bit, hundred ... 1 that have appeared.

Now give the general law of N directly, for example, when n=abced five digits, we analyze hundred C, there are three kinds of cases:

1) c = = 0 when, for example, 12013, at this time the hundred appeared 1 is: 00 100 ~ 00 199, 01 100~01 199,......,11 100~ 11 199, a total of 1200, obviously this has a high number of digits, and is affected by the current number of digits;

2) c = = 1 when, for example, 12113, at this time the hundreds of 1 of the affirmation includes the case of c=0, in addition to consider the situation of low, namely: 00100 ~ 00113 A total of 114,

3) C >= 2, such as 12213, at this time hundreds of 1 is: 00 100 ~ 00 199, 01 100~01 199,......,11 100~ 11 199,12 100 ~ 12 199, a total of 1300, this has a high number of figures, in fact, is added one, and and multiplied by the current number of digits

So you can write the following code:

    public int Countdigitone (int n) {        Long count = 0;        Long factor = 1;                while (N/factor! = 0) {            long lower = n% Factor;            Long cur = (n/factor)%;            Long higher = n/(factor *);                        if (cur < 2) {                count + = higher * factor;                if (cur = = 1) count + = lower+1;            } else {                count + = (higher+1) * factor;            }            Factor *=;        }        return (int) count;    
It's really hard to analyze the problem!


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Leetcode-number of Digit One (number of programming beauty-1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.