Leetcode-989 Add to Array-Form of Integer(数组形式的整数加法)

大数加法,没什么好说的,乱搞过了

 1 #define maxn 1000000
 2 #define _for(i,a,b) for(int i = (a);i < (b);i ++)
 3 #define pb push_back
 4 
 5 class Solution
 6 {
 7     public:
 8         vector<int> addToArrayForm(vector<int>& A, int K)
 9         {
10             vector<int> B;
11             while(K)
12             {
13                 B.pb(K%10);
14                 K/=10;
15             } 
16             reverse(A.begin(),A.end());
17             vector<int> rnt(max(A.size(),B.size())+1);
18             for(int i = 0;i < max(A.size(),B.size());i ++)
19             {
20                 if(i<min(A.size(),B.size()))
21                     rnt[i] = A[i]+B[i];
22                 else if(A.size()>B.size())
23                     rnt[i] = 0 +A[i];
24                 else
25                    rnt[i] = 0+B[i];
26             }
27             for(int i = 0 ;i < rnt.size();i ++)
28             {
29                 if(rnt[i]>=10)
30                 {
31                     rnt[i] -= 10;
32                     rnt[i+1] ++;
33                 }
34             }
35             if(rnt[rnt.size()-1]==0)
36                 rnt.pop_back();
37             reverse(rnt.begin(),rnt.end());
38             return rnt;
39         }
40 };

 

posted @ 2019-02-10 18:17  Asurudo  阅读(211)  评论(0编辑  收藏  举报