LeetCode 66. Plus One

来源:互联网 发布:voip软件电话 编辑:程序博客网 时间:2024/05/18 02:21

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.


The logic is very clear. Plus one.

vector<int> plusOne(vector<int>& digits) {    int size = digits.size();    digits[size - 1] += 1;   // lowest bit is at the end.    for(int j = size - 1; j >= 1 && digits[j] == 10; j--) {        digits[j] = digits[j] % 10;        digits[j-1] += 1;    }    if(digits[0] == 10) {        digits[0] = 0;        digits.insert(digits.begin(), 1);    }    return digits;}



0 0