Skip to content

Commit 72f3133

Browse files
authored
Create 989. Add to Array-Form of Integer 15 feb (#93)
989. Add to Array-Form of Integer
2 parents 200e5b7 + ee59267 commit 72f3133

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
class Solution {
3+
public:
4+
5+
vector<int> addToArrayForm(vector<int>& num, int k) {
6+
7+
int carry = 0;
8+
int j = num.size() - 1;
9+
10+
while(j >= 0)
11+
{
12+
13+
int sum = num[j] + (k % 10) + carry;
14+
k /= 10;
15+
16+
num[j--] = sum % 10;
17+
carry = sum/10;
18+
19+
}
20+
21+
while(k > 0)
22+
{
23+
24+
int sum = (k % 10) + carry;
25+
k /= 10;
26+
27+
num.insert(num.begin(), sum%10);
28+
carry = sum/10;
29+
30+
}
31+
32+
if(carry > 0) num.insert(num.begin(), carry);
33+
34+
return num;
35+
}
36+
};

0 commit comments

Comments
 (0)