Plus One

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:

 1 vector<int> plusOne(vector<int>& digits) {
 2        
 3         vector<int>result(digits);
 4         bool flag = true;
 5         
 6         int index = result.size()-1;
 7         while ( index >= 0 )
 8         {
 9             result[index] = (result[index]+1)%10;
10             flag = (result[index--]==0)?true:false;
11             
12             if (!flag)
13                 return result;
14         }
15         
16         if (flag)
17         {
18             result.insert(result.begin(),1);
19         }
20         return result;
21     }

 

posted @ 2015-06-16 23:44  Rosanne  阅读(220)  评论(0编辑  收藏  举报