Skip to main content
  1. Problem Solving Solutions/

Plus One LeetCode Solution

·1 min read
Leetcode
Mayukh Datta
Author
Mayukh Datta
Table of Contents

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

Solution in C++
#

class Solution { public: vector plusOne(vector& digits) { int n = digits.size();

    // if last digit is not 9,increment it by 1
    if(digits\[n-1\] != 9) {
        digits\[n-1\] += 1;
    } else {
        // if last digit is 9
        for(int i=n-1; i>=0; i--) {
            if(digits\[i\] == 9 && i != 0) {
                digits\[i\] = 0;
            }else if(digits\[i\] == 9 && i == 0) {
                digits\[i\] = 0;
                digits.insert(digits.begin(), 1);
            }else{
                digits\[i\] += 1;
                break;
            }
        }
    }        
    return digits;
}

};