Share C++ and Python code, actually add whatever number is same


  • 0
    W
    class Solution {
    public:
        vector<int> plusOne(vector<int>& digits) {
            int carry=0; int n=digits.size();
            digits[n-1]+=1;
            for(int i=n-1;i>-1;i--){
                int a=digits[i];
                digits[i]=(a+carry)%10;
                carry=(a+carry)/10;
            }if(carry>0) digits.insert(digits.begin(),1);
            return digits;
        }
    };
    
    
    
    
    class Solution(object):
            def plusOne(self, digits):
                """
                :type digits: List[int]
                :rtype: List[int]
                """
                n,carry=len(digits),0
                digits[n-1]+=1
                for i in range(n-1,-1,-1):
                    digits[i],carry=(digits[i]+carry)%10,(digits[i]+carry)/10
                if carry==1:
                    digits.insert(0,1)
                return digits

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.