Plus One LeetCode Problem solution

66. Plus One

Given a non-empty array of decimal digits representing a non-negative integer, increment one to the integer.
The digits are stored such that the most significant digit is at the head of the list, and each element in the array contains a single digit.
You may assume the integer does not contain any leading zero, except the number 0 itself.

Example 1:

Input: digits = [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.

Example 2:

Input: digits = [4,3,2,1]
Output: [4,3,2,2]
Explanation: The array represents the integer 4321.

Example 3:

Input: digits = [0]
Output: [1]

Constraints:

  • 1 <= digits.length <= 100
  • 0 <= digits[i] <= 9

C++ Solution:

vector<int> plusOne(vector<int>& digits) {
        int carry=1;
    vector <int> v;
    while(digits.size()>0){
        int x = digits.back();
        digits.pop_back();
        x = x + carry;
        v.insert(v.begin(), x%10);
        carry = x/10;
    }
    if (carry>0){
        v.insert(v.begin(), carry);
    }
    return v;
    }

I am explain this code in very easy manner and still you guys confuse to understand this then contact me.

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart