leetcode - Plus One

来源:互联网 发布:java agent 哨兵 编辑:程序博客网 时间:2024/06/05 11:34

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.

class Solution {public:    std::vector<int> plusOne(std::vector<int> &digits) {        std::vector<int> result;int t = 1;for(int i = digits.size() - 1; i >= 0; i--){t += digits[i];result.insert(result.begin(),t % 10);t /= 10;}if(t > 0){result.insert(result.begin(),t);}return result;    }};


0 0
原创粉丝点击