题目链接:Leetcode 66
大致描述一下题目:
数组表示的数字加一,返回加一后的值,数组首元素存放的高位。
[1,2,3] + 1 -> [1,2,4]
解题思路:
基本的加法进位逻辑
附上解题代码:(C++)
/*******************************************************************************
Copyright © 2018-20xx Qiao Chuncheng, All Rights Reserved.
File name: 066[加一].cpp
Author: Qiao Chuncheng
Version: v1.0
Date: 2018-04-17
*******************************************************************************/
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
int len = digits.size();
int num = 1;
for (int i = len - 1; i >= 0; i--)
{
digits[i] += num;
if (digits[i] < 10)
break;
else
digits[i] = 0;
if (i == 0 && digits[0] == 0)
{
vector<int> a;
a.push_back(1);
for (int j = 0; j < len; j++)
a.push_back(0);
return a;
}
}
return digits;
}
};