Given a number represented as an array of digits, plus one to the number.
» Solve this problem
class Solution {
public:
vector<int> plusOne(vector<int> &digits) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<int> answer;
int c = 1;
for (int i = digits.size() - 1; i >= 0; i--) {
c += digits[i];
answer.insert(answer.begin(), c % 10);
c = c / 10;
}
if (c > 0) {
answer.insert(answer.begin(), c);
}
return answer;
}
};
数组加一算法实现
本文介绍了一种将数组形式表示的数字加一的算法实现。通过遍历数组从最低位开始逐位相加,并处理进位操作,最终返回加一后的数组。此方法适用于整数加法场景。
1520

被折叠的 条评论
为什么被折叠?



