Offer to come, dig friends take it! I am participating in the 2022 Spring Recruit Punch card activity. Click here for details.

728. Self-divisor

This brush topic diary 20, force deduction is: 728. Self-divisor, simple

I. Title Description:

For the last problem of the homework, let’s finish this brush problem with a simple and relaxed problem. Let’s feel happy and brush the problem with pay at work

Simple problems are not easy, many times when we see simple problems, we always consider how to use better skills to complete a problem, if there is no skill, we will also use violence to solve

2. What ideas does this question examine? What’s your thinking?

What is the meaning of the passage?

  • We need to understand the definition of an autodivisor. For example, 128, given by weight, is an autodivisor, because it satisfies the requirement that 128 should be divisible by 1, 2 and 8
  • It is important to note that the self-divisor cannot contain 0. That is to say, numbers like 108,120 do not satisfy the self-divisor
  • They give us a data interval, and we need to filter out the autodivisor within that interval

If we know what a self-divisor is and what the problem needs, we can figure out how to give it to it. Let’s use the following example to deduce:

Example: Left = 1, right = 22

Take some numbers for example

So, in this case, we can just foolishly take a number of digits and see if it satisfies the condition, and if every digit satisfies the condition, then the number satisfies the condition

So in this case, we just have to figure out how many of the numbers that they give us, how many of them satisfy this condition, and we can just figure them out

Three, coding

According to the above logic and analysis, we can translate the code as follows. When translating the code, we need to pay attention to whether there is a 0 in the number. If there is a 0, then the number is not self-divisor directly

The encoding is as follows:

// Define a helper function to check whether the incoming number is a self-divisor
func helper(num int) bool {
    for tmp := num; tmp>0; tmp=tmp/10 {
        // Check if the given digit is divisible by each of its digits
        if x:=tmp%10; x==0|| num % x ! =0{
            return false}}return true
}

func selfDividingNumbers(left int, right int) []int {

    res := make([]int.0)
    // Check the list to see which numbers fit the criteria
    for i:=left; i<=right; i++ {
        if helper(i) {
            res = append(res,i)
        }
    }

    return res
}
Copy the code

Looking at the above code, it is still relatively clear, the logic of coding is consistent with the logic of deduction, but we can see that this is a relatively stupid method, violent solution, there are better skills in the future, I will share a wave

Iv. Summary:

The time complexity of this problem is O(nlog right), where n is the number of intervals given, and ** space complexity is O(1), where we introduce only constant level space consumption

Original address: 728

Today is here, learning, if there is a deviation, please correct

Welcome to like, follow and favorites

Friends, your support and encouragement, I insist on sharing, improve the quality of the power

All right, that’s it for this time

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

I am Nezha, welcome to like, see you next time ~