您的位置:首页 > 其它

Plus One

2015-06-16 23:44 218 查看
Description:

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.

Code:

vector<int> plusOne(vector<int>& digits) {

vector<int>result(digits);
bool flag = true;

int index = result.size()-1;
while ( index >= 0 )
{
result[index] = (result[index]+1)%10;
flag = (result[index--]==0)?true:false;

if (!flag)
return result;
}

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