1. 程式人生 > >Leetcode-989 Add to Array-Form of Integer(數組形式的整數加法)

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

數組 leet lee div pub form 整數 ret min

大數加法,沒什麽好說的,亂搞過了

 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 };

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