
-Code
class Solution {
public int[] plusOne(int[] digits) {
for (int i = digits.length - 1; i >= 0 ; i--) {
digits[i]++;
// 현제 인덱스 값이 10보다 작으면 바로 반환
if (digits[i] < 10) return digits;
// 큰경우 해당 위치 0으로 변경
digits[i] = 0;
}
// 어차피 1만 더하는 것이라 1칸만 늘어남
int[] result = new int[digits.length + 1];
// 즉, 늘어나는 부분이 무조건 1
result[0] = 1;
return result;
}
}
문제를 이상하게 이해해서 어렵게 풀다가 문제점을 찾아보니 쉽게 해결했습니다.
'Beakjoon&프로그래머스 > Java' 카테고리의 다른 글
| [백준/Java] 27445번 Gorani Command (0) | 2025.11.22 |
|---|---|
| [백준/Java] 34687번 라면 끓여 먹자 야호 (0) | 2025.11.21 |
| [LeetCode/Java] 58. Length of Last Word (0) | 2025.11.20 |
| [LeetCode/Java] 35. Search Insert Position (0) | 2025.11.20 |
| [LeetCode/Java] 28. Find the Index of the First Occurrence in a String (0) | 2025.11.20 |