Jian Zhi 43- Find the regular solution for the number of 1 in the integer

Question details

The narrative and perfunctory on Niukenet here are still more detailed. The input is a positive integer, because n can be very large, so the time complexity must be considered, not O(N).
Insert picture description here

answer

The number of 1s For
this question, the first thing to note is that the question is to count the number of 1s, not the number of 1s. So 11 is not once, but twice, because there are two ones. Special attention is needed here, otherwise it is easy to make mistakes.

Need to find a law.
For a number n, we naturally can not go to traverse each number in 1-n, to count the number of 1 of each number, and accumulate, this time complexity is too high, it will time out.
So we need a regular way to solve it, to quickly know how many 1 are there in n

Laws
Let’s study 0-9 and find that there are 1 1
10-19, 11 1s here (11 has two 1s), here can be divided into 10-19, there is 1 1 in the tens place, and 11 is There is a 1 in the ones place,
20-29, and a 1
···The
ones behind are only a 1,
so there are 20 1s in 0-99,
and then in the same way, let’s count 0-999 (within 1000). How many 1s are
divided into 000-099, 100-199,...900-999
to calculate this way, first count the tens and the ones of 1, which is 20*10=200
and 100-199 is that there are 1 in the hundreds, so There are 100 1s,
so 200+100 in 000-999 = 300 1s

Then by analogy, there are 4k 1s within 10000 (not including 10000 itself), and 5w 1s within 100,000
so that the law can be summarized, 100···00 (level 0), then unit = level*10^( level-1) 1

In this way, we have obtained the law of the number of 1s in this whole number

But here we only consider the situation of 1000···0.
As for X00···00, it
is actually easy to handle, that is, X00···000. When X>1, it must contain 1xxxxxxx, so We first count the basic number
X*unit, and then add the number starting with 1, and the number of 1xxxxxxxx will do, that is, 10^level 1s. The total sum is ok

So it is to find the number of 1 in a very whole number

For the logic of a number that is not so integral,
such as the number 9667, how should it be calculated?
The logic is as follows

  • For example, 9667, 9 k, then first is 9*300+1k = 3700
  • Then it is 667, 6 hundred, so it is 6*20+100 = 220
  • Then it is 67, which is 6*1+10 = 16
  • Then it is 7 and it is 1
  • The result is 3700+220+16+1=3937

Let’s go from high to low, and take one digit separately. One
thing to pay attention to.
When my high digit is 1, the number of 1s I have is the sum of the number of 1s in the high digit and the value of the low digit,
such as 1677. For this number,
I need to calculate the number of all 1s within 1k,
and then the number of 1s in the thousand digits of 1000-1677, (emphasis) The
final is the hundreds, tens, and ones of 1000-1677. number

Code

public class Solution {
    
    
    public int NumberOf1Between1AndN_Solution(int n) {
    
    
        if(n<1) return 0;
        int res = 0;
        //进行拆分
        while(n!=0){
    
    
            int mod = n%((int)Math.pow(10,(int)Math.log10(n)));
            String str = String.valueOf(n);
            if(n>9&&str.charAt(0)=='1') res+=mod+1;//保证一定是十位数以上的才行
            res += getOneNum(n-mod);
            n = mod;
        }
        return res;
    }

    //获得整的数中的1的个数
    private int getOneNum(int n){
    
    
        if(n<10) return n>=1 ? 1 : 0;//个位数
        //下面都是整的,10,200,3000这种
        int level = 0;//统计级别
        while(n>=10){
    
    
            n /= 10;
            level++;
        }
        //计算基本单位
        int unit = level*(int)Math.pow(10,level-1);
        int res = n * unit +(n>1?(int)Math.pow(10,level):0) ;
        return res; 
    }
    
}

Guess you like

Origin blog.csdn.net/qq_34687559/article/details/113823530