[Brush questions diary] 357. Count the number of numbers with different numbers

Get into the habit of writing together! This is the 10th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .

[Brush questions diary] 357. Count the number of numbers with different numbers

The 30th article of this writing diary is titled: 357. Count the number of numbers with different numbers , medium

1. Topic description:

Brothers, it's a bit late to check the loopholes today. I came back and continued to brush the questions. Fortunately, I read this question in the morning, and I already have a certain idea. Let's take a look now.

2. What idea does this question examine? What is your thinking?

The title information of this question is relatively clear and the content is relatively small, that is, the title gives a specific range, the left is closed and the right is open, and the type and number of different numbers in each digit in the range are found

The maximum range of this number is 10 to the 8th power , that is, 1 followed by 8 0s

The topic is clear. At first glance, it seems that the idea is not very clear, but I may not be able to hold it for a while.

It's okay, we can reason about it and understand that this is actually a math problem. I still remember that we solved such a math problem when we were in high school or junior high school.

Then we can get the same

When n = 3, when there are 3 digits, there are 9*9*8= 648 kinds that meet the conditions, plus the previous 91 kinds, then there are 739 kinds

In this way, we can understand that this is actually a mathematical problem, a problem of permutation and combination

Now this math problem can be taken out, and then we can translate the code according to the idea

3. Coding

According to the above logic and analysis, we can translate it into the following code. Here we need to pay attention to be clear. When there are multiple digits, we need to pay attention to the number of different digits that can appear on each digit.

The encoding is as follows:

func countNumbersWithUniqueDigits(n int) int {
    if n == 0 {
        return 1
    }
    if n == 1 {
        return 10
    }
    // 初始化好当 n等于 1 的时候,结果是 10中,并且当前的 1 位数的时候,符合条件的种数是 9,因为 0-9 中,第 1 位 不能是 0 开头
    res, cur := 10, 9
    for i := 0; i < n-1; i++ {
        // 第 1 位数,有 9 种,第 2 位有 9 种, 第 3 位 有 8 种,以此类推
        cur *= 9 - i
        res += cur
    }
    return res
}
复制代码

4. Summary:

The amount of code for this problem is relatively small, and the time complexity is not difficult to see. It is O(n) , the number of loops is n-1 times, the space complexity here is O(1) , and we introduce a constant-level space consume

Original title address: 357. Count the number of numbers with different numbers

I am here today, what I have learned, if there are any deviations, please correct me

Welcome to like, follow, favorite

Friends, your support and encouragement are the motivation for me to persist in sharing and improve quality

Okay, here it is this time

Technology is open, and our mentality should be open. Embrace change, live in the sun, and move forward.

I'm the little devil boy Nezha , welcome to like, follow and collect, see you next time~

Guess you like

Origin juejin.im/post/7085361575556546590