[LeetCode] Plus One

来源:互联网 发布:15年广东省的经济数据 编辑:程序博客网 时间:2024/05/01 16: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.

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

怎么看着略蛋疼呢,好像用STL做太简单了。。。额

0 0