Plus One

Given a number represented as an array of digits, plus one to the number.

C++:
01 vector<int> plusOne(vector<int> &digits) {
02       int n=digits.size();
03       vector <int> result(n);
04       int carry=1;
05       for(int i=n1;i>=0;i){
06           if(carry+digits[i]>9){
07               carry=1;
08               result[i]=carry+digits[i]10;
09           }
10           else{
11               result[i]=carry+digits[i];
12               carry=0;
13           }
14       }
15       if(carry==1)
16           result.insert(result.begin(),1);
17       return result;
18 }
Advertisements
This entry was posted in Array and linked list. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s