[Leetcode]66.Plus One
来源:互联网 发布:武汉发展前景知乎 编辑:程序博客网 时间:2024/06/04 00:39
[leetcode] 66. Plus One
题目链接:https://leetcode.com/problems/plus-one/
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) { int flag = 1; for(auto it = digits.rbegin(); it != digits.rend();it++){ int sum = *it + flag; flag = sum / 10; *it = sum % 10; } if(flag) digits.insert(digits.begin(),flag); return digits; }};
阅读全文