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

【 brush question diary 】504

504. Seven base numbers, simple

I. Title Description:

Ii. Analysis of Ideas:

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

After looking at the description and example of this problem, it is relatively simple. For the topic of base 7, and base 2, base 3 and so on are similar ideas and practices. We can get the following information

  • I want to convert an integer to the corresponding base 7 number, and the output is a string
  • If the integer is negative, the first character of the resulting string is-
  • Use the toss and turn division to process, for n-base can be handled

Following the example given above, we can deduce it using the toss and turn division method, which I remember as the method of learning:

If you divide 100 by 7, it becomes 14, with a remainder of 2

Divide 14 by 7, which is 2, and you have a remainder of 0

If you divide 2 by 7, you get 0, and you have a remainder of 2

And then the remainder in base 7 is going to be 202, is there a misunderstanding, is it going to be top down, or is it going to be bottom up

So we could have another number, like 102

The calculation method is the same as above, but I won’t repeat it here

And finally 102 is 204, not 402, and we can see from the picture that the remainder is a combination of the remainder from the bottom up

2. Try coding

So, based on the diagram and logic above, it’s not hard to write code that looks like this

When we’re coding, we need to think about the case where num is zero, and the case where num is negative

func convertToBase7(num int) string {
    // Handle negative numbers
    res := ""
    yu := 0
    var tmp int
    if num < 0{
       tmp = 0 - num
       res = "-"
    }else{
        tmp = num
    }
    
    tmpStr := make([]string.0)
    for {
         yu = tmp % 7	/ / to take over
         tmp = tmp / 7  / / o
         tmpStr = append(tmpStr,strconv.Itoa(yu))
         if tmp == 0 {
             break}}for i:=len(tmpStr)- 1; i>=0; i--{ res += tmpStr[i] }return res

}
Copy the code

The code above is also very simple

  • The first step is to deal with the negative case
  • The second step deals with the relationship between the remainder and the quotient and puts the remainder into an array
  • The third step is to iterate backwards through the array to get the result

Iv. Summary:

This is a normal and simple way, the official way is backward + iteration, you can see the following official code, there is another inspiration, interested can see leetcode-cn.com/problems/ba…

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 ~