Plus One

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

C++:
01 class Solution {
02 public:
03     vector<int> plusOne(vector<int> &digits) {
04         // Start typing your C/C++ solution below
05         // DO NOT write int main() function
06         vector<int> result;
07         int n=digits.size();
08         int carry=1;
09         for(int i=n1;i>=0;i)
10         {
11             int t=digits[i]+carry;
12             if(t>=10)
13             {
14                 carry=1;
15                 t=t10;
16             }
17             else
18             {
19                 carry=0;
20             }
21             result.push_back(t);
22         }
23         if(carry!=0)
24             result.push_back(carry);
25         int m=result.size()1;
26         for(int i=0;i<=m/2;i++)
27         {
28             int p=result[i];
29             result[i]=result[mi];
30             result[mi]=p;
31         }
32         return result;
33     }
34 };
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