[E模拟] lc66. 加一(模拟+高精度)

1. 题目来源

链接:66. 加一

2. 题目解析

前导题:

就是高精度加法。模板题。

  • 时间复杂度 O ( n ) O(n) O(n)
  • 空间复杂度 O ( 1 ) O(1) O(1)

代码:

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

        if (t) digits.push_back(t);

        reverse(digits.begin(), digits.end());
        
        return digits;
    }
};

猜你喜欢

转载自blog.csdn.net/yl_puyu/article/details/111873052