addToArrayForm-Integer addition in array form

Title

For non-negative integer X, the array form of X is an array formed by each digit from left to right. 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 integer X+K.

Example 1:

Input: A = [1,2,0,0], K = 34
Output: [1,2,3,4]
Explanation: 1200 + 34 = 1234

Example 2:

Input: A = [2,7,4], K = 181
Output: [4,5,5]
Explanation: 274 + 181 = 455

Example 3:

Input: A = [2,1,5], K = 806
Output: [1,0,2,1]
Explanation: 215 + 806 = 1021

Example 4:

Input: A = [9,9,9,9,9,9,9,9,9,9], K = 1
Output: [1,0,0,0,0,0,0,0,0, 0,0]
Explanation: 9999999999 + 1 = 10000000000

prompt:

1 <= A.length <= 10000
0 <= A[i] <= 9
0 <= K <= 10000
如果 A.length > 1,那么 A[0] != 0

Related Topics array

Problem-solving ideas

1. First of all, we cannot determine the length of the two numbers, but according to the meaning of the question, k is an integer, and the maximum value of k will not exceed 10000, so if the length of k is greater than or equal to the A array, it means that the two numbers are the same The addition will not be greater than INTEGET.MAX_VALUE
2. If the length of the array is greater than k, it may be greater than the largest integer. The
detailed problem solving will be presented in the code one by one (pay attention to the operation and detailed processing)

Code demo:

class Solution {
    public  List<Integer> addToArrayForm(int[] A, int K) {
        int alen = A.length-1;
        List<Integer> res=new ArrayList<>();
        int flag=0;//标志位,如果两个对应位置相加超过个位数,则为1,代表进1.
        //这一重循环主要是比较k的长度和alen的长度大小,从低位到高位运算,将结果保存到res中
        while (K!=0&&alen!=-1)
        {
            if(K%10+A[alen]+flag<10)
            {
                res.add(K%10+A[alen]+flag);
                flag=0;
            }
            else {
                res.add((K%10+A[alen]+flag)%10);
                flag=1;
            }
            K=K/10;
            alen--;
        }
        //数组元素长度小于等于k的长度
        if (alen==-1)
        {
            //求出剩余整数k与flag的值
            //(k的细节处理-------------------------------------------------)
            K=K+flag;
            //新的k是0的情况,意思是所有数都耗光了
            if(K==0)
            {
                Collections.reverse(res);
                return res;
            }
            //k不是0,新k有剩余
            while (K!=0)
            {
                res.add(K%10);
                K=K/10;
            }
        }
        //数组元素还存在的情况,此时k应该为0,flag不知道有没有
        else {
            while (alen!=-1)
            {
                if(A[alen]+flag<10)
                {
                    res.add(A[alen]+flag);
                    alen--;
                    flag=0;
                    //(当flag可以舍弃时,直接break,跳到下面,注意细节
                   // -------------------------------------)
                    break;
                }else {
                    res.add((A[alen]+flag)%10);
                    flag=1;
                    alen--;
                }
            }
            while (alen!=-1)
            {
                res.add(A[alen]);
                alen--;
            }

            if(flag==1)
                res.add(flag);
        }
        Collections.reverse(res);
        return res;
    }
}

Result presentation

The info
answer was successful:
execution time: 4 ms, defeating 86.63% of Java users
Memory consumption: 40.1 MB, defeating 34.70% of Java users

Guess you like

Origin blog.csdn.net/tangshuai96/article/details/112980118