Leetcode Number of Digit one

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.

Hint:

1. Beware of overflow.

Problem Solving Ideas:

To see the answer. Very burning brain, did not see very clearly.

This problem is actually equivalent to a law-finding problem. So to find out the rules, let's start by listing all the numbers that contain 1, and count each of the 10, as follows:

Number of

1           numbers with 1                                                                             Number range

1                    1                                                                                         [1, 9]

11 10 11 12 13 14 15 16 17 18 19 [10, 19]

1 21 [20, 29]

1                    31                                                                                       [in]

1                    41                                                                                       [+]

1                    51                                                                                       []

1                    61                                                                                       [up]

1                    71                                                                                       [+]

1                    81                                                                                       [+]

1 91 [90, 99]

11 100 101 102 103 104 105 106 107 108 109 [100, 109]

21 110 111 112 113 114 115 116 117 118 119 [110, 119]

11 120 121 122 123 124 125 126 127 128 129 [120, 129]

...                  ...                                                                                  ...

As we can see from the above list, there are only 1 of the numbers within 100, except for 11 ' 1 ' between 10-19. If we don't consider the more than 10 ' 1 ' on the [10, 19] interval, then we can add two more to the number of 10 digits (plus 1) on any 1-digit, 10-digit number. For example, 56 have (5+1) +10=16. How to know whether to add more than 10, we have to see whether the number on the 10-bit is greater than or equal to 2, is to add the extra 10 ' 1 '. Then we can use (x+8)/10来 to determine whether a number is greater than or equal to 2. The same is true for three-bit numbers, except for the 10 numbers that are extra between [110, 119] and the remaining 10 ' 11 ', the same method can be used to determine and accumulate the number of 1.

Java Code:

 Public class Solution {    publicint countdigitone (int  n) {        int res = 0 , a = 1, b = 1;          while (n > 0) {            + = (n + 8)/ten * a + (n% = = 1? b:0)            ; + = n% * A;             *=;             /=;        }         return res;    }}

Reference:

1. http://www.cnblogs.com/grandyang/p/4629032.html

2. Https://leetcode.com/discuss/44281/8-lines-o-log-n-c-java-python

Leetcode Number of Digit one

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.