0
点赞
收藏
分享

微信扫一扫

66. Plus One - LeetCode



​题目:Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.You may assume the integer do not contain any leading zero, except the number 0 itself.The digits are stored such that the most significant digit is at the head of the list.思路:题目本质上是简单的大数加法,只需要遍历数组,注意逢9进1即可代码:​

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

Tips:在运行

Custom Testcase

时,如果输入是[999999],expect answer会报错:Line 17: std::vector<int> Solution::plusOne(std::vector<int>&): Assertion `carry == 0' failed.,这个问题已经提交了



举报

相关推荐

0 条评论