알고리즘

LeetCode : : 66번 Plus One - C++ 풀이

green333 2022. 1. 1. 12:48
728x90
SMALL

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

 

Plus One - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

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
class Solution {
public:
    vector<int> plusOne(vector<int>& digits) {
        int len = digits.size();
        
        // 9,9 --> 9,0 --> 1, 0, 0
        // 9,9,9 --> 9,9,0 --> 9,0,0 --> 1,0,0,0
        // 4,9 --> 4,0 --> 5,0
        // 1,2,3 --> 1,2,4
        
        for(int i = len; i > 0 ; i--){
            if(digits[i-1== 9 && i-1 == 0){
                digits[i-1= 0;
                digits.insert(digits.begin(),1);
                return digits; 
            }
            else if(digits[i-1== 9){
                digits[i-1= 0;
                continue;
            }
            else{
                digits[i-1= digits[i-1+ 1;
                return digits;
            }
        }
    return digits;
    }
};
cs

 

문제 이해 잘하기 !!!!
LIST