File tree 1 file changed +8
-15
lines changed
1 file changed +8
-15
lines changed Original file line number Diff line number Diff line change @@ -8,25 +8,18 @@ class Solution {
8
8
reverse (num1.begin (), num1.end ());
9
9
reverse (num2.begin (), num2.end ());
10
10
11
- for (int i = 0 ;i < num1.length ();i++)
12
- {
13
- int carry (0 );
14
- for (int j = 0 ;j < num2.length ();j++)
15
- {
11
+ for (int i = 0 ;i < num1.length ();i++) {
12
+ int carry = 0 ;
13
+ int j = 0 ;
14
+ while (j < num2.size () || carry > 0 ) {
16
15
int digit = result[i+j] - ' 0' ;
17
- int num = (num1[i] - ' 0' ) * (num2[j] - ' 0' );
16
+ int v = j < num2.size () ? num2[j] : ' 0' ;
17
+ int num = (num1[i] - ' 0' ) * (v - ' 0' );
18
18
int res = digit + num + carry;
19
19
result[i+j] = (res % 10 ) + ' 0' ;
20
20
carry = res / 10 ;
21
- }
22
-
23
- int index = i + num2.size ();
24
- while (carry)
25
- {
26
- int digit = result[index ] - ' 0' ;
27
- int res = digit + carry;
28
- result[index ] = (res % 10 ) + ' 0' ;
29
- carry = res / 10 ;
21
+
22
+ j++;
30
23
}
31
24
}
32
25
You can’t perform that action at this time.
0 commit comments