출처 : https://leetcode.com/problems/plus-one/
You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading 0's.
Increment the large integer by one and return the resulting array of digits.


class Solution {
public int[] plusOne(int[] digits) {
ArrayList<Integer> arrayList = new ArrayList<>();
boolean carry = false;
if (digits.length == 1) {
if (digits[0] == 9) {
return new int[]{1, 0};
} else {
return new int[]{digits[0] + 1};
}
} else {
for (int d = digits.length - 1; d >= 0; d--) {
if (d == digits.length - 1) {
// 올림 없을 때
if (digits[d] != 9) {
arrayList.add(0, digits[digits.length - 1] + 1);
for (int a = digits.length - 2; a >= 0; a--) {
arrayList.add(0, digits[a]);
}
}
// 올림 있을 때
else {
arrayList.add(0, 0);
carry = true;
}
} else {
if (carry && digits[d] + 1 != 10) {
arrayList.add(0, digits[d] + 1);
carry = false;
for (int a = d - 1; a >= 0; a--) {
arrayList.add(0, digits[a]);
}
} else if (carry && digits[d] + 1 == 10) {
arrayList.add(0, 0);
carry = true;
if (d == 0) {
arrayList.add(0, 1);
}
}
}
}
}
int[] result = new int[arrayList.size()];
for (int a = 0; a < arrayList.size(); a++) {
result[a] = arrayList.get(a);
}
return result;
}
}