Solution 30: Number of "1" occurrences from 1 to n

Source: Internet
Author: User

Problem description

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.

Solution Ideas


1. Traverse + Count Method (can be converted to string count): timeout;

2. Mathematical method: Find the law. The law is:

----------------------------------------------------------------------------------------------------------

Write a function f (n) that returns the number of "1" occurrences between 1 and N, such as f (12) = 5.

Suppose n = abcde, where a,b,c,d,e are numbers on each digit of the decimal number n respectively. If you want to calculate the hundreds on the show 1

will be affected by 3 factors: the number on the hundred, the number below the Hundred (low), and the number above the Hundred (higher).

If the number on the hundred is 0, you can see that the hundreds may appear 1 times higher, such as 12 013, you can know

Hundreds of 1 of cases may be 100-199,1 100-1 199,......,11 100-11 199, a total of 1 200. That is

is determined by a higher number (12) and is equal to the higher digit (x) of the current digit (100).

If the number on the hundred is 1, then you can know that the hundreds may appear 1 times not only by the higher impact, but also by the low impact,

That is, the higher and the lower the joint decision. For example, 12 113, which is affected by higher levels, 1 of the hundreds appear to be 100-199,1 100

-1 199,......,11 100-11 199, a total of 1 200, as in the first case above, equal to the higher number (x)

The number of preceding digits (100). But it is also affected by the low, hundreds of 1 of the situation is 12 100-12 113, a total of 114, equal to the low number

(113) +1.

If the number on the hundred is greater than 1 (that is, 2-9), then the hundreds may appear on the number of times 1 is also determined by a higher level, such as 12 213, then

Hundreds of 1 of cases are: 100-199,1 100-1 199,......,11 100-11 199,12 100-12 199 of 1300 total

, and equals the higher number +1 (12+1) x current digits (100).

--------------------------------------------------------------------------------------------------------------- -

For example, a three-digit number, such as:

Program

The second method:

public class Numofones {public    int countdigitone (int n) {    long cnt = 0;    Long base = 1;                while (n >= base) {            Long low = n Base;            Long cur = (n/base)%;            Long high = n/(base *);                        if (cur = = 0) {cnt + = high                * BASE;            } else if (cur = = 1) {cnt + =                Base * High + low + 1;            } else {                c NT + = base * (high + 1);            }                        Base *=;        }                return (int) cnt;    }}

It is important to note that a long or even larger number of unitsis required to store CNT, otherwise it is easier to cross the border.

Solution 30: Number of "1" occurrences from 1 to n

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.