当前位置:编程学习 > C/C++ >>

leetcode_question_66 Plus One

Given a number represented as an array of digits, plus one to the number.
 
 
vector<int> plusOne(vector<int> &digits) {  
        // Start typing your C/C++ solution below  
        // DO NOT write int main() function  
        int length = digits.size();  
        if(length==0)  
            return digits;  
        bool carry = true;  
        int index = length-1;  
        while(carry && index >= 0)  
        {  
            digits[index] += carry;  
            if(digits[index] > 9)  
            {digits[index] = 0;carry = true;index--;}  
            else  
            {carry = false; break;}  
        };  
        if(carry)  
            digits.insert(digits.begin(), 1);  
        return digits;  
    }  

 

 
补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,