[LeetCode] 233. The number of number 1 (same sword refers to Offer43)

1. Title

Given an integer n, count the number of occurrences of the number 1 in all non-negative integers less than or equal to n.

Example:

输入: 13
输出: 6 
解释: 数字 1 出现在以下数字中: 1, 10, 11, 12, 13

Two, solve

1. Brute force cracking

Ideas:

After reading the topic, you can write the code directly, but the method times out .

Code:

class Solution {
    
    
    public int countDigitOne(int n) {
    
    
        int cnt = 0;
        for (int i = 1; i <= n; i++) {
    
    
            String str = Integer.toString(i);
            for (char c : str.toCharArray()) {
    
    
                if (c == '1') cnt++;
            }
        }
        return cnt;
    }
}

Time complexity: O (nlogn) O(nlogn)O ( n l o g n )
space complexity: O (logn) O(logn)O ( l o g n )

2. Dynamic planning

version 1

Ideas:

Main reference 3, the specific analysis is as follows:

The number Y is the number of X digits, each of the digits (one, ten, hundred, thousand, ten thousand...) is composed of nxnx − 1… n 2 n 1 n_xn_{x−1}…n_2n_1nxnx1n2n1. among them

  • 1 number : cnt;
  • Current position : ni n_ini, Marked as cur;
  • High bit : nxnx − 1… ni + 1 n_xn_{x−1}…n_{i+1}nxnx1ni+1, Marked as high;
  • Low bits : ni − 1… n 2 n 1 n_{i−1}…n_2n_1ni1n2n1, Marked as low;
  • Bit factor : 1 0 i 10^i10i , marked as factor.

1. if cur = 0,then cnt = high×digit

eg Find the tens of 2304, that is, when facor=10, the number of 1 occurrences.

2304

另一种推导:十位固定位1
1) 千位选{
    
    01},百位{
    
    01...9},个位{
    
    01...9},cnt = 2*10*10=200;
2) 千位选2,百位{
    
    012},个位{
    
    01...9},cnt = 1*3*10=30;

总计:200+30=230.  可推出公式:cnt  = high×digit

2. if cur = 1,then cnt = high×digit+low+1

eg Find the tens of 2314, that is, when facor=10, the number of 1 occurrences.

2314

另一种推导:十位固定位1
1) 千位选{
    
    01},百位{
    
    01...9},个位{
    
    01...9},cnt = 2*10*10=200;
2) 千位选2,     百位{
    
    012},    个位{
    
    01...9},cnt = 1*3*10=30;
3)千位选2,     百位选3,          个位{
    
    01...4},cnt = 5;

总计:200+30+5=235.  可推出公式:cnt  = high×digit+low+1

3. if cur > 1,then cnt = (high+1)×digit

eg Find the tens of 2324, that is, when facor=10, the number of 1 occurrences.

2324

另一种推导:十位固定位1
1) 千位选{
    
    01},百位{
    
    01...9},个位{
    
    01...9},cnt = 2*10*10=200;
2) 千位选2,     百位{
    
    0123},  个位{
    
    01...9},cnt = 1*4*10=40;

总计:200+40=240.  可推出公式:cnt  = (high+1)×digit

Code:

class Solution {
    
    
    public int countDigitOne(int n) {
    
    
       int res = 0;
       long a = 0;
       long b = 0;
       for(long m=1;m<=n;m*=10){
    
    
           a = n/m;
           b = n%m;
           if(a % 10 > 1){
    
    
               res += a/10 * m + m;
           }else if( a%10 == 1){
    
    
               res += a/10 * m + b + 1;
           }else{
    
    
               res += a/10 * m;
           }
       }
        return res;
    }
}

Time complexity: O (logn) O(logn)O ( l o g n )
space complexity: O (1) O(1)O ( 1 )

Version 2

Idea: To further optimize the above derivation formula.
Code:

// V1.0
class Solution {
    
    
    public int countDigitOne(int n) {
    
    
        if (n <= 0) return 0;
        long ones = 0;
        for (long i = 1, q = n; i <= n; i *= 10, q /= 10) {
    
    
            long pre = n / (i * 10), cur = q % 10, suf = n % i;
            ones += pre * i;
            ones += (1 < cur ? i : (1 == cur ? suf + 1: 0));
        }
        return (int) ones;
    }
}

// V2.0
class Solution {
    
    
    public int countDigitOne(int n) {
    
    
	    if (n <= 0) return 0;
        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;
    }
}

Time complexity: O (logn) O(logn)O ( l o g n )
space complexity: O (1) O(1)O ( 1 )

Three, reference

1. The number of number
1 2. 4+ lines, O(log n), C++/Java/Python
3. Interview question 43. The number of 1 to n integers (clear illustration)
4. Runtime: 0 ms, faster than 100.00% of Java online
5. Comparison of the efficiency of int to String and String to int methods in Java

Guess you like

Origin blog.csdn.net/HeavenDan/article/details/109187378