반응형
66. 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.
예제
Input: digits = [1,2,3]
Output: [1,2,4]
Input: digits = [4,3,2,1]
Output: [4,3,2,2]
Input: digits = [9]
Output: [1,0]
제약 조건
- $1 \leq digits.length \leq 100 $
- $0 \leq digits[i] \leq 9 $
digits
does not contain any leading0
's.
코드
/**
* @param {number[]} digits
* @return {number[]}
*/
var plusOne = function(digits) {
// Number.MAX_SAFE_INTEGER를 초과하는 정수가 있으므로 BigInt로 풀기
return String(BigInt(digits.join('')) + BigInt(1)).split('').map(Number);
};
참고
반응형
'개발 > 리트코드' 카테고리의 다른 글
[리트코드] 36. Valid Sudoku(유효한 스도쿠) (1) | 2025.06.16 |
---|---|
[리트코드] 22. Generate Parentheses (0) | 2025.06.10 |