MCPcopy Create free account
hub / github.com/ROUTINE-STUDY/Algorithm / Sanghoo

Class Sanghoo

LeetCode/Array/66. Plus One/Sanghoo.java:6–41  ·  view source on GitHub ↗

https://leetcode.com/problems/plus-one/

Source from the content-addressed store, hash-verified

4 * https://leetcode.com/problems/plus-one/
5 */
6public class Sanghoo {
7
8 public static int[] plusOne(int[] digits) {
9 // 끝자리가 9라면 계산 필요
10 if(digits[digits.length-1] == 9) {
11 // 뒤에서부터 9의 개수 카운팅
12 int cnt = 0;
13 for(int i=digits.length-1; i>=0; i--) {
14 if(digits[i] == 9) cnt++;
15 else break;
16 }
17
18 // 요소 모두 9라면 배열의 길이 증가 필요, 맨 앞에만 1로 바꿔주고 int[] 기본값은 0
19 if(cnt == digits.length) {
20 int[] arr = new int[digits.length+1];
21 arr[0] = 1;
22 return arr;
23 } else { // 모두 9가 아니라면 요소 앞은 +1이 되고 뒷자리는 0으로 변경
24 int len = digits.length-1;
25 while(cnt >= 0) {
26 if(cnt == 0) {
27 digits[len]++; break;
28 }
29 digits[len] = 0;
30 --len; --cnt;
31 }
32 return digits;
33 }
34 } else {
35 digits[digits.length-1]++;
36 return digits;
37 }
38
39 }
40
41}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected