[Leetcode Data Structure Algorithm Question] Integer Addition in Array Form (Sequence Table)

Topic content:

For a nonnegative integer X, the array form of X is an array of each digit in left-to-right order.
For example, if X = 1231, then its array form is [1,2,3,1].
Given the array form A of the non-negative integer X, return the array form of the integers X+K.

insert image description here
leetcode topic link (click to jump):


Method 1: Reduction and recombination method (with pit)

1) First restore the array to numbers. For example: [1,2,0,0] is restored to 1200
2) Add the restored number to X to get the result. For example: 1200 + 34 = 1234
3) Convert the result to an array. For example, 1234 is converted to [1,2,3,4]

Function interface implementation

int* addToArrayForm(int* num, int numSize, int k, int* returnSize){
    
    
    //1)将数组还原成数字
    int i = 0;
    int A = 0;
    while(i < numSize){
    
    
        A += num[i]*pow(10,numSize-i-1);
        i++;
    }
    //2)将数字相加得到结果
    int sum = A + k;
    //3)将结果转化成数组形式
    //计算sum的位数
    int tmp = sum;
    i = 1;
    while(tmp /= 10){
    
    
        i++;
    }
    *returnSize = i;
    int* arr = (int*)malloc((*returnSize) * sizeof(int));
    while(i > 0){
    
    
        arr[i-1] = sum % 10;
        sum /= 10;
        i--;
    }
    return arr;
}

Test result: The normal use case can pass the test, but when the length of the array is longer than the range that the int type can represent, an error occurs. The value range of int is: -2^31 to 2^31-1, ie -2147483648 to 2147483647
insert image description here
insert image description here


Method 2: Array bit-by-bit addition

Since the method of restoring the array to a number and then adding it doesn't work, we can naturally think of another way to "convert the number into an array, then array + array".
1) Convert the number k into an array. For example, 34 becomes [3,4]
2) When adding arrays to arrays, the carry relationship between numbers should be considered.
3) Determine the new array after addition and its length

Function interface implementation

int* addToArrayForm(int* num, int numSize, int k, int* returnSize){
    
    
    //计算k的位数
    int count = 1;
    int temp = k;
    while (temp /= 10)
    {
    
    
        count++;
    }
    //比较count和numSize大小,确定要开数组的空间
    int Asize = count > numSize ? count + 1 : numSize + 1;
    int* array = (int*)calloc(Asize, sizeof(int));
    //进行运算
    int i = 0;
    int j = Asize - 1;
    for (i = numSize - 1; i >= 0 || k > 0; )
    {
    
    
        if (i >= 0)
        {
    
    
            array[j] += num[i] + k % 10;
            if (array[j] >= 10)
            {
    
    
                array[j - 1]++;
                array[j] -= 10;
            }
            i--;
        }
        else
        {
    
    
            array[j] += k % 10;
            if (array[j] >= 10)
            {
    
    
                array[j - 1]++;
                array[j] -= 10;
            }
            i--;
        }
        k /= 10;
        j--;
    }
    if (array[0] == 0)//从前往后移位
    {
    
    
        for (i = 0; i < Asize - 1; i++)
        {
    
    
            array[i] = array[i + 1];
        }
        *returnSize = Asize - 1;
    }
    else
    {
    
    
        *returnSize = Asize;
    }
    return array;
}

insert image description here


It's not easy to be original, everyone, give a like, comment + follow~
(Little hands like, the water goes back and forth, every test must pass, everything goes smoothly~)

Guess you like

Origin blog.csdn.net/QIYICat/article/details/122271278