Leetcode学习笔记:#989. Add to Array-Form of Integer

    xiaoxiao2024-12-15  54

    Leetcode学习笔记:#989. Add to Array-Form of Integer

    For a non-negative integer X, the array-form of X is an array of its digits in left to right order. For example, if X = 1231, then the array form is [1,2,3,1].

    Given the array-form A of a non-negative integer X, return the array-form of the integer X+K.

    实现:

    public List<Integer> addToArrayForm(int[] A, int K){ List<Integer> res = new LinkedList<>(); for(int i = A.length-1; i >= 0; --i){ res.add(0,(A[i] + K) % 10); K = (A[i] + K) /10; } while(K > 0){ res.add(0, K); K /= 10; } return res; }

    思路: 从后往前遍历,保留多加出来的1的位置

    最新回复(0)