[Brush questions diary] 67. Binary summation

Offer arrives, dig friends to pick up! I am participating in the 2022 Spring Recruitment Check-In Event, click to view the event details .

[Brush questions diary] 67. Binary summation

The 15th article of this review diary is titled: 67. Binary summation , simple

1. Topic description:

After exercising in the evening, continue to look at the questions. This question, the title is very clear, the description is very short, and it seems to be a simple question . This is a simple question. Let's change our heads .

Mutual confidence - I don't know where the confidence comes from

But we can't take it lightly, and we still have to carefully analyze the key content and test points given by this question.

2. Thought analysis:

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

The information given in the title has these key points:

  • The given binary numbers are all strings , and what we return also needs to be strings
  • The numbers in the string are only 0 and 1 , and the given string does not contain leading 0, and the length of the array ranges from 1 to 10 to the 4th power, so there is no need to consider the abnormal input for this question.

Check out our example: a = "1010", b = "1011"

Looking at this example, the length of the **a array is the same as the length of the b array, ** and the middle involves carry, and the highest bit also involves carry

In the following example, also note that the length of the a array is less than the length of the b array, or the length of the b array is less than the length of the a array . By default, the above is the a array, and the following is the b array.

In general, these points need to be considered:

  • len (a) == len (b)
  • len (a) <len (b)
  • len (a)> len (b)
  • Handling of carry

3. Coding

According to the above logic and analysis, we can translate it into the following code

The encoding is as follows:

func addBinary(a string, b string) string {
    lenA,lenB := len(a),len(b)
    n := 0
    if lenA > lenB {
        n = lenA
    }else{
        n = lenB
    }

    res := ""
    jinwei := 0
    // 使用较长数组的长度来控制循环
    for i:=0; i<n; i++ {
        // 对应位数 a 数组上有值,那就进行相加
        if i<lenA {
            jinwei += int(a[lenA-i-1] - '0')
        }
        // 对应位数 b 数组上有值,那就进行相加
        if i<lenB {
            jinwei += int(b[lenB-i-1] - '0')
        }
        // a 数组和 b 数组对应位置上相加后,需要填在该位置的值 
        res = strconv.Itoa(jinwei % 2) + res
        // 需要向前进位的数据
        jinwei = jinwei / 2
    }

    // 判断最后是否需要进位
    if jinwei > 0{
        res = "1" + res
    }

    return res
}
复制代码

After reading the above coding, you can find that the coding is completely based on the thinking and analysis before coding.

To see the big from the small, before coding, we really need to make the scheme design clear and practical, and break down every detail and blind spot one by one, so as to reduce the risk after coding

4. Summary:

The time complexity of this question is obviously O(n), n is the length of the longer array, and the space complexity is O(1), which introduces constant-level memory consumption

Original title address: 67. Binary summation

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/7079773017902956558