您的位置:首页 > 其它

leetcode 66. Plus One

2017-11-01 10:14 316 查看
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.

题目:一个整数按照每一位存在数组里面,然后返回整数 + 1之后的数组

所以:一位一位处理,不全部提出来

class Solution {
public:
vector<int> plusOne(vector<int>& dig)
{
int flag = 0;
int pos = dig.size();

while ( (--pos) >= 0 )
{
if (pos == dig.size() - 1)
{
if (dig[pos] < 9)
{
dig[pos]++;
return dig;
}
else
{
dig[pos] = 0;
flag = 1;
}
}
else
{
if (dig[pos] + flag < 10)
{
dig[pos]++;
return dig;
}
else
dig[pos] = 0;
}
}

if (flag==1)
{
dig.insert(dig.begin(), 1);
}
return dig;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: