본문 바로가기
Problem Solving

1-5. Plus One (LeetCode 66)

by tls1107 2021. 7. 7.
728x90
반응형

 


 

class Solution {
    public int[] plusOne(int[] digits) {
        int[] answer = new int[digits.length + 1];

        digits[digits.length - 1] += 1;
        for(int i = 1 ; i <= digits.length ; i++){
            if(digits[digits.length - i] == 10){
                if(digits[0] == 10){
                    digits[digits.length - i] = 0;
                    answer[0] = 1;
                    for(int j = 0 ; j < digits.length ; j++){
                        answer[j+1] = digits[j];
                    }
                    return answer;
                }
                digits[digits.length - i] = 0;
                digits[digits.length - i-1] += 1;
            }
        }
        return digits;
    }
}

https://leetcode.com/problems/plus-one/

 

728x90
반응형

댓글