개발/리트코드

[리트코드] 66. Plus One

DOTBAAAM 2025. 6. 10. 21:52
반응형

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 leading 0'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);
};

LeetCode - 66. Plus One

참고

Number.MAX_SAFE_INTEGER

반응형