문제
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.
Example 1:
Input: digits = [1,2,3] Output: [1,2,4] Explanation: The array represents the integer 123. Incrementing by one gives 123 + 1 = 124. Thus, the result should be [1,2,4]. Example 2:
Input: digits = [4,3,2,1] Output: [4,3,2,2] Explanation: The array represents the integer 4321. Incrementing by one gives 4321 + 1 = 4322. Thus, the result should be [4,3,2,2]. Example 3:
Input: digits = [9] Output: [1,0] Explanation: The array represents the integer 9. Incrementing by one gives 9 + 1 = 10. Thus, the result should be [1,0].
Constraints:
1 <= digits.length <= 100 0 <= digits[i] <= 9 digits does not contain any leading 0’s.
내 풀이
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class Solution {
public int[] plusOne(int[] digits) {
//sol : if the last digit is not 9, just plue one to the last digits and return.
// if the last digit is 9, check the previous digit by recursive function.
// 마지막 숫자가 9가 아니라면 그냥 +1 해서 리턴, 9 라면 재귀함수로 앞의 숫자들 검사해간다. (앞의 단위 숫자가 9 인경우 또 다음 앞 숫자로 올림 해야하기에 재귀 함수 사용)
// 공통 로직: 마지막 숫자에 +1
// common logic : plus one to the last digit.
digits[digits.length-1] = digits[digits.length-1] +1;
// 마지막 숫자 +1 != 10 -> 숫자 올림 필요 없어서 바로 리턴
// If the last digit + 1 does not exceed 10, return it immediately.
if(digits[digits.length-1]!=10){
return digits;
}else{
// 한칸씩 앞으로 가면서 숫자 올림 필요한지 확인
//check recursively. (example input : {9,9,9} -> {1,0,0,0})
digits = isUpscale(digits, digits.length-1);
}
return digits;
}
//recursive function that check if this index's digit + 1 exceeds 10.
// 인덱스와 배열 넣어 앞 단위 숫자 검사하는 함수
public int[] isUpscale(int[] digits, int index){
if(digits[index]==10){
digits[index]=0;
if(index -1 >= 0){ // if it has still previous digit, recursive search. /// 앞의 단위 숫자 남아있으면 계속 재귀 돌림
digits[index-1]=digits[index-1]+1;
digits=isUpscale(digits, index-1);
}else{ // if the fisrt number (index 0) is 10, make answer array's size increse and deep copy of last digits. 0번째 숫자가 10되면 자릿수 올림해야하기에 배열 크기 늘리고 딥카피
int[] newDigits = new int[digits.length+1];
newDigits[0]=1;
for(int i=1; i<digits.length+1; i++){
newDigits[i]=digits[i-1];
}
return newDigits;
}
}
return digits;
}
}