LeetCode:Plus One

来源:互联网 发布:java正则表达式全匹配 编辑:程序博客网 时间:2024/05/21 11:32

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
原创粉丝点击