标签:style class blog code color os
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.
编程注意一点:如果vector的位数变了,那么要重新定义新的size比原来的大1的vector,因为原来的vector只能容纳原来那么多元素。
class Solution { public: vector<int> plusOne(vector<int> &digits) { int len = digits.size(); if(digits[len-1]<9){ digits[len-1]++; return digits; }//end if int index = len-1; while(digits[index]==9) index--; if(index != -1){ digits[index]++; for(int i = index+1;i<len;i++) digits[i]=0; return digits; }else{ vector<int> result(len+1,1);//注意:结果数比原来多一位,需要定义新的vector来装结果 for(int j=1;j<len+1;j++) result[j]=0; return result; }//end if } };
[LeetCode] Plus One,布布扣,bubuko.com
标签:style class blog code color os
原文地址:http://www.cnblogs.com/Xylophone/p/3796950.html