LeetCode 66. Plus One

来源:互联网 发布:射雕英雄传 知乎 编辑:程序博客网 时间:2024/06/07 23:52

问题描述:

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.

问题大意:用一个数组表示非负数,可以理解为一个用数组表示的大数,然后用这个大数加上一,返回结果。

AC代码:

 vector<int> plusOne(vector<int>& digits)     {        int n = digits.size();        int val = 1;        for(int i = n-1;i>=0;i--)        {            digits[i] = digits[i] + val;            val = digits[i] /10;//判断是否有进位            digits[i] = digits[i] % 10;        }        if(val > 0)            digits.insert(digits.begin(),1);        return digits;    }


0 0
原创粉丝点击