Plus One @LeetCode

PROBLEM :

Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.

You may assume the integer do not contain any leading zero, except the number 0 itself.

The digits are stored such that the most significant digit is at the head of the list.

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

class Solution {
public:
    vector<int> plusOne(vector<int>& digits) {
        int carry=1 ;

        int size=digits.size() ;
        for(int j=size-1;j>=0;j--){
            digits[j]=digits[j]+carry ;
            carry=digits[j]/10 ;
            digits[j]=digits[j]%10 ;
        }
 
        if(carry)
            digits.insert(digits.begin(),carry) ;
           
        return digits ;
    }
};

--------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )