Plus One - LeetCode

注意点

  • 考虑开头数字有进位的情况

解法

解法一:如果当前数字是9就变为0,否则就+1,并return。时间复杂度O(n)

class Solution {
public:
    vector<int> plusOne(vector<int>& digits) {
        int i = digits.size()-1;
        while(i >= 0)
        {
            if(digits[i] == 9) digits[i] = 0;
            else
            {
                digits[i] += 1;
                return digits;
            }
            i--;
        }
        if (digits[0] == 0) digits.insert(digits.begin(), 1);
        return digits;
    }
};

Plus One - LeetCode

小结

  • 链表是很常见的一种数据结构,要花点时间专门研究一下。

相关文章:

  • 2022-01-29
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-07-03
  • 2021-12-03
  • 2021-06-22
  • 2022-01-27
  • 2021-09-20
  • 2021-08-20
相关资源
相似解决方案