作者:天飞的鹊桥会大美女 | 来源:互联网 | 2023-10-10 11:03
题目Givenanon-negativenumberrepresentedasanarrayofdigits,plusonetothenumber.Thedigitsarest
题目
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.
//给一个用数组表示的数,加1后还是数组表示
算法
复杂度:O(N)
// 从右往左,如果到[0] 位还有进位,则需要(n.length_1)的数组
class Solution {
public:vector<int> plusOne(vector<int>& digits) {vector<int> tp(digits), ret;tp[tp.size()-1]&#43;&#43;;for(int i&#61;tp.size()-1;i>0;i--)if(tp[i]>&#61;10){tp[i]%&#61;10;tp[i-1]&#43;&#43;;}else break;if(tp[0]>&#61;10){ret.push_back(1);tp[0]%&#61;10;}for(int i&#61;0;i!&#61;(int)tp.size();&#43;&#43;i)ret.push_back(tp[i]);return ret;}
};