首页 > 其他 > 详细

Plus One

时间:2015-06-23 11:32:26      阅读:235      评论:0      收藏:0      [点我收藏+]

题目描述:

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.

  英语不好,第一句愣是没看懂。其实就是用一个数组表示一个整数,比如数字123,数组对应存放1、2、3三个数字。

solution:

vector<int> plusOne(vector<int>& digits) {
    if(digits.empty())
        return digits;
    int i = digits.size() - 1;
    while(i >= 0 && digits[i] == 9)
        digits[i--] = 0;
    if(i >= 0)
        digits[i]++;
    else
    {
        digits[0] = 1;
        digits.push_back(0);
    }
    return digits;
}

 

Plus One

原文:http://www.cnblogs.com/gattaca/p/4594895.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!