LeetCode 989. Add to Array-Form of Integer
考点 | 难度 |
---|---|
Array | Easy |
题目
The array-form of an integer num
is an array representing its digits in left to right order.
For example, for num = 1321
, the array form is [1,3,2,1]
.
Given num
, the array-form of an integer, and an integer k
, return the array-form of the integer num + k
.
思路
第一个iteration算个位,第二个算十位,以此类推。
答案
public List<Integer> addToArrayForm(int[] num, int k) {
final LinkedList<Integer> result = new LinkedList<>();
int len = num.length - 1;
while(len >= 0 || k != 0){
if(len >= 0){
k += num[len--];
}
result.addFirst(k % 10);
k /= 10;
}
return result;
}