关闭

LeetCode:Plus One

标签: leetcodePlus One
316人阅读 评论(0) 收藏 举报
分类:

Plus One

Total Accepted: 73167 Total Submissions: 233149 Difficulty: Easy

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.

Subscribe to see which companies asked this question

Hide Tags
 Array Math
Hide Similar Problems
 (M) Multiply Strings (E) Add Binary















code:

class Solution {
public:
    vector<int> plusOne(vector<int>& digits) {
        int len = digits.size();
        reverse(digits.begin(), digits.end());
        int carry=1;
        for(int i=0;i<len && carry;i++) {
            int tmp =digits[i] + carry;
            carry = tmp / 10;
            tmp %= 10;
            digits[i]=tmp;
        }
        if(carry) digits.push_back(carry);
        reverse(digits.begin(),digits.end());
        return digits;
    }
};


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:347708次
    • 积分:6374
    • 等级:
    • 排名:第4010名
    • 原创:314篇
    • 转载:1篇
    • 译文:0篇
    • 评论:25条
    博客专栏
    最新评论