Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- 누룽지소금빵
- 가챠
- 지쿠악스
- 블라인드박스
- 프라모델
- 발더스게이트
- 바질토마토뭐시기
- 건담
- 제프딕슨
- 건담헤드
- 우리시대의역설
- javascript
- 눈알빠지겠네
- 유루건
- 지리데칼
- 롱라이플
- 코딩테스트
- DIY
- 밥무하마드
- 코테
- 미앤아이
- 포켓몬
- 노노그램
- 맛집
- 30mf
- 게임
- 취미
- 메탈퍼즐
- 발더스모드
- LeetCode
Archives
- Today
- Total
.Zzumbong
[leetCode/JS] 66. Plus One 본문
난이도 [ 😊 ] Easy
문제 설명
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.
내 솔루션
/**
* https://leetcode.com/problems/plus-one
* @param {number[]} digits
* @return {number[]}
*/
var plusOne = function (digits) {
for (let i = digits.length - 1; i >= 0; i--) {
digits[i]++;
if (digits[i] < 10) {
return digits;
}
digits[i] = 0;
}
digits.unshift(1);
return digits;
};
감상평
- 처음엔 reduce로 모든 배열을 더해서 숫자 -> 스트링으로 변경 후 -> 다시 배열 변경 하려고 했는데 숫자가 100 자리 이상이라 오류가 났음.
- 배열을 직접 건들이는 방식으로 해결.

'coding test > leetCode' 카테고리의 다른 글
| [leetCode/JS] 1970. Last Day Where You Can Still Cross (1) | 2025.12.31 |
|---|---|
| [leetCode/JS] 3074. Apple Redistribution into Boxes (0) | 2025.12.29 |
| [leetCode/JS] 756. Pyramid Transition Matrix (1) | 2025.12.29 |
| [leetCode/JS] 1351. Count Negative Numbers in a Sorted Matrix (0) | 2025.12.29 |
| [leetCode/JS] 3477. Fruits Into Baskets II (1) | 2025.08.05 |
Comments
