본문 바로가기
Problem Solving/LeetCode

[LeetCode] 66. Plus One - Java

by graycode 2026. 5. 20.

 문제 링크

 

Plus One - LeetCode

Can you solve this real interview question? 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-

leetcode.com

 

 풀이 코드

public class Solution {

    public int[] plusOne(int[] digits) {
        for (int i = digits.length - 1; i >= 0; digits[i--] = 0) {
            if (digits[i] < 9) {
                digits[i]++;
                return digits;
            }
        }

        int[] arr = new int[digits.length + 1];
        arr[0] = 1;

        return arr;
    }

}

댓글