June 21, 2022 leetcode daily check-in - 67. Binary summation [C language detailed explanation, clear notes]

1. Topic description and requirements

67. Binary summation

topic description

Given you two binary strings, return their sum (in binary representation).

The input is a non- empty string containing only numbers 1and 0.

example

Example 1

输入: a = "11", b = "1"
输出: "100"

Example two

输入: a = "1010", b = "1011"
输出: "10101"

hint

  • Each string consists of only characters  '0' OR  '1' .
  • 1 <= a.length, b.length <= 10^4
  • Strings that  "0" do not contain leading zeros.

2. Problem-solving ideas

General idea:

The simplest idea is to convert binary to decimal and then add and convert to binary, but overflow may occur. Therefore, we still choose to dynamically allocate storage space and re-apply for an array space to store the sum of each bit addition. At this time, an additional flag upadate should be set to record whether the previous bit has been entered by 1, because if there is a 1, the result of the addition will be different, so it is necessary to discuss in multiple situations. Moreover, binary operations are added from the last digit like decimal operations, so when accessing strings, start from the last digit. At the same time, judge the length of the string, use it as the basis to apply for the array space for storing the added sum, and then access it. Just entering the loop upadate=0, because it is the first addition, and then if you want to enter 1, let upadate =1, if not used, store 0/1 in the new array accordingly, then indexA-- and indexB--, if upadate=1 at this time, you need to consider it separately, if both are 1, you need to enter 1 again, Then leave 1, if one is 1, enter 1, leave 0, if all are 0, you don’t need to enter 1, assign upadate to 0, and so on until there is an empty one in a or b, and then it is right or not That is mainly to judge whether there is a 1, otherwise it is a direct assignment. Finally, it is necessary to judge whether the upadate is equal to 0. If not, it means that the last addition still needs to enter 1 but there is no number to be added. At this time, it is necessary to apply for a new array and make its first digit equal to 1, the rest is passed from the merged array. If not, just assign the values ​​of the merged array to the new array, and finally return the new array.

Specific steps:

1. Define and judge whether to enter an upadate flag, the initial value is 0
2. Find the length of the string and judge whether it is empty
3. Select one of the lengths as the basis to apply for a new array storage space
4. Traverse and judge
5. Finally judge to create a new array and return

3. Specific code

char * addBinary(char * a, char * b){
    int upadate=0;//是否进1的标志,初始为0
    int lenA=strlen(a);//字符串a的长度
    int lenB=strlen(b);//字符串b的长度
    if(lenA==0&&lenB==0)//如果两个字符串都为空则返回其中任意一个就行
    {
        return a;//等价return b;
    }
    char *combine;//如果两个字符串的长度不一的话就要选择长的那个作为基准申请新的存储空间将二者进行合并
    int combine_len;//合并的字符串的长度
    if(lenA>=lenB)//字符串a比b长
    {
        combine=(char*)malloc(sizeof(char)*lenA);//申请长度为lenA的存储空间作为合并后的存储空间
        combine_len=lenA;//将lenA赋给合并后的字符串长度
    }
    else//字符串b比a长
    {
        combine=(char*)malloc(sizeof(char)*lenB);//申请长度为lenB的存储空间作为合并后的存储空间
        combine_len=lenB;//将lenB赋给合并后的字符串长度
    }
    //index是定位的意思,以下三个分别是指向三个字符串数组的下标
    //因为相加计算是从最后一位进行计算的,所以三个变量的初始值都是长度-1
    int indexA=lenA-1;
    int indexB=lenB-1;
    int indexC=combine_len-1;
    while (indexA>=0||indexB>=0)//当二者有一个不为空时就进入循环
    {
        if((indexA>=0)&&(indexB>=0))//字符串a和b都存在的时候
        {
            if(upadate)//如果前一步已经进过1了
            {
                if(a[indexA]=='1'&&b[indexB]=='1')//此时两位都为1的话则又要进1,同时加上上次进1,应当是为3,最后剩下1,所以相加后这一位应当是1,同时又要进一位,所以upadate还是1
                {
                    upadate=1;
                    combine[indexC]='1';
                }
                else if(a[indexA]=='1'||b[indexB]=='1')//如果只有一个为1,加上上次进1则为2进1,此时因为要进1所以upadate=1,而相加后这一位为0
                {
                    upadate=1;
                    combine[indexC]='0';
                }
                else//两者都为0的话相加就等于进的1,此时不会再进一,所以upadate=0.
                {
                    upadate=0;
                    combine[indexC]='1';
                }
            }
            else//如果前一步不需要进1
            {
                if(a[indexA]=='1'&&b[indexB]=='1')
                {
                    upadate=1;//进一
                    combine[indexC]='0';
                }
                else if(a[indexA]=='1'||b[indexB]=='1')
                {
                    combine[indexC]='1';
                }
                else
                {
                    combine[indexC]='0';
                }
            }
            indexA--;//前移
            indexB--;
        }
        else if(indexA>=0)//只剩下数组a
        {
            if(upadate)//如果后一位进1了
            {
                if(a[indexA]=='1')
                {
                    combine[indexC]='0';
                    upadate=1;
                }
                else
                {
                    combine[indexC]='1';
                    upadate=0;
                }
            }
            //以下两种就是没有进一的情况,a的值就是和的值,因为b数组加完了
            else if(a[indexA]=='1')
            {
                combine[indexC]='1';
            }
            else
            {
                combine[indexC]='0';
            }
            indexA--;
        }
        else//只剩下数组b
        {
            if(upadate)//如果后一位进1了
            {
                if(b[indexB]=='1')
                {
                    combine[indexC]='0';
                    upadate=1;
                }
                else
                {
                    combine[indexC]='1';
                    upadate=0;
                }
            }
            else if(b[indexB]=='1')
            {
                combine[indexC]='1';
            }
            else
            {
                combine[indexC]='0';
            }
            indexB--;
        }
        indexC--;
    }
    int i;
    char *new_combine=(char*)malloc(sizeof(char)*1000);
    if(upadate)//如果相加完后最后一位需要进一
    {
        for(i=0;i<combine_len;i++)
        {
            new_combine[i+1]=combine[i];//将合并后的值赋给新数组
        }
        new_combine[0]='1';//由于到最后一位(字符串的第一位,前面是后序遍历相加)需要进1,所以第一位为1
        new_combine[combine_len+1]='\0';//结束标志
    }
    else //不需要进1,直接将合并数组的值全部赋值到新数组
    {
        for(i=0;i<combine_len;i++)
        {
            new_combine[i]=combine[i];
        }
        new_combine[combine_len]='\0';//结束标志
    }
    return new_combine;
}

Guess you like

Origin blog.csdn.net/m0_59800431/article/details/125387607