File tree 1 file changed +22
-1
lines changed
1 file changed +22
-1
lines changed Original file line number Diff line number Diff line change 2
2
// Space: O(m + n)
3
3
4
4
class Solution {
5
+ public:
6
+ string multiply (string num1, string num2) {
7
+ string result (num1.size () + num2.size (), ' 0' );
8
+ for (int i = num1.size () - 1 ; i >= 0 ; --i) {
9
+ for (int j = num2.size () - 1 ; j >= 0 ; --j) {
10
+ int sum = (num1[i] - ' 0' ) * (num2[j] - ' 0' ) + (result[i + j + 1 ] - ' 0' );
11
+ result[i + j + 1 ] = sum % 10 + ' 0' ;
12
+ result[i + j] += sum / 10 ;
13
+ }
14
+ }
15
+ int pos = result.find_first_not_of (' 0' );
16
+ if (pos != string::npos) {
17
+ return result.substr (pos);
18
+ }
19
+ return " 0" ;
20
+ }
21
+ };
22
+
23
+ // Time: O(m * n)
24
+ // Space: O(m + n)
25
+ class Solution2 {
5
26
public:
6
27
string multiply (string num1, string num2) {
7
28
const auto char_to_int = [](const char c) { return c - ' 0' ; };
@@ -32,7 +53,7 @@ class Solution {
32
53
// Time: O(m * n)
33
54
// Space: O(m + n)
34
55
// Define a new BigInt class solution.
35
- class Solution2 {
56
+ class Solution3 {
36
57
public:
37
58
string multiply (string num1, string num2) {
38
59
return BigInt (num1) * BigInt (num2);
You can’t perform that action at this time.
0 commit comments