66. 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.
题目大意:将一个数字的各位都放在一个数组中,给这个数字加1,求得到的新数组。
高位在前。
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
int len = digits.size();
for(int i = len - 1; i >= 0;i--)
{
if(digits[i] + 1 < 10)
{
digits[i] = digits[i] + 1;
break;
}
else
{
digits[i] = 0;
if(i == 0)
{
digits.clear();
digits.push_back(1);
for(int j = 0 ;j < len; j++)
digits.push_back(0);
}
}
}
return digits;
}
};
2016-08-08 23:27:58
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。