Leetcode 66 Plus One

来源:互联网 发布:数据交换规范 编辑:程序博客网 时间:2024/06/06 03:54

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

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

模拟大数加法加一,

注意判断首位是否有进位!

class Solution {public:    vector<int> plusOne(vector<int>& digits) {        int add=1;        for(int i=digits.size()-1;i>=0;i--)        {             digits[i]=(digits[i]+add)%10;             if(digits[i]!=0) break;        }        if(digits[0]==0) digits.insert(digits.begin(),1);        return digits;    }};


1 0
原创粉丝点击