47
47
``` python
48
48
class Solution :
49
49
def multiply (self , num1 : str , num2 : str ) -> str :
50
- def add (s1 , s2 ):
51
- n1, n2 = len (s1), len (s2)
52
- i = carry = 0
53
- res = []
54
- while i < max (n1, n2) or carry > 0 :
55
- a = int (s1[n1 - i - 1 ]) if i < n1 else 0
56
- b = int (s2[n2 - i - 1 ]) if i < n2 else 0
57
- carry, t = divmod (a + b + carry, 10 )
58
- res.append(str (t))
59
- i += 1
60
- return ' ' .join(res[::- 1 ])
61
-
62
- if ' 0' in [num1, num2]:
63
- return ' 0'
64
- n1, n2 = len (num1), len (num2)
65
- ans = ' '
66
- for i in range (n1):
67
- a = int (num1[n1 - i - 1 ])
68
- t = ' '
69
- for j in range (n2):
70
- b = int (num2[n2 - j - 1 ])
71
- t = add(t, str (a * b) + ' 0' * j)
72
- ans = add(ans, t + ' 0' * i)
73
- return ans
50
+ def mul (b , i ):
51
+ j, t = m - 1 , 0
52
+ while j >= 0 or t:
53
+ if j >= 0 :
54
+ a = int (num1[j])
55
+ t += a * b
56
+ res[i] += t % 10
57
+ if res[i] >= 10 :
58
+ res[i] %= 10
59
+ res[i + 1 ] += 1
60
+ i, j = i + 1 , j - 1
61
+ t //= 10
62
+
63
+ m, n = len (num1), len (num2)
64
+ res = [0 ] * (m + n)
65
+ for i in range (n):
66
+ b = int (num2[n - 1 - i])
67
+ mul(b, i)
68
+ while len (res) > 1 and res[- 1 ] == 0 :
69
+ res.pop()
70
+ return ' ' .join([str (v) for v in res[::- 1 ]])
74
71
```
75
72
76
73
### ** Java**
@@ -80,43 +77,114 @@ class Solution:
80
77
``` java
81
78
class Solution {
82
79
public String multiply (String num1 , String num2 ) {
83
- if (Objects . equals(num1, " 0" ) || Objects . equals(num2, " 0" )) {
84
- return " 0" ;
80
+ int m = num1. length(), n = num2. length();
81
+ int [] res = new int [m + n];
82
+ for (int i = 0 ; i < n; ++ i) {
83
+ int b = num2. charAt(n - 1 - i) - ' 0' ;
84
+ mul(num1, b, i, res);
85
85
}
86
- int n1 = num1. length(), n2 = num2. length();
87
- String ans = " " ;
88
- for (int i = 0 ; i < n1; ++ i) {
89
- int a = num1. charAt(n1 - i - 1 ) - ' 0' ;
90
- String t = " " ;
91
- for (int j = 0 ; j < n2; ++ j) {
92
- int b = num2. charAt(n2 - j - 1 ) - ' 0' ;
93
- StringBuilder sb = new StringBuilder (String . valueOf(a * b));
94
- for (int k = 0 ; k < j; ++ k) {
95
- sb. append(0 );
96
- }
97
- t = add(t, sb. toString());
86
+ StringBuilder ans = new StringBuilder ();
87
+ for (int v : res) {
88
+ ans. append(v);
89
+ }
90
+ while (ans. length() > 1 && ans. charAt(ans. length() - 1 ) == ' 0' ) {
91
+ ans. deleteCharAt(ans. length() - 1 );
92
+ }
93
+ return ans. reverse(). toString();
94
+ }
95
+
96
+ private void mul (String A , int b , int i , int [] res ) {
97
+ for (int j = A . length() - 1 , t = 0 ; j >= 0 || t > 0 ; -- j) {
98
+ if (j >= 0 ) {
99
+ int a = A . charAt(j) - ' 0' ;
100
+ t += a * b;
98
101
}
99
- StringBuilder sb = new StringBuilder (t);
100
- for (int k = 0 ; k < i; ++ k) {
101
- sb. append(0 );
102
+ res[i++ ] += t % 10 ;
103
+ if (res[i - 1 ] >= 10 ) {
104
+ res[i - 1 ] %= 10 ;
105
+ ++ res[i];
102
106
}
103
- ans = add(ans, sb. toString());
107
+ t /= 10 ;
108
+ }
109
+ }
110
+ }
111
+ ```
112
+
113
+ ### ** C++**
114
+
115
+ ``` cpp
116
+ class Solution {
117
+ public:
118
+ string multiply(string num1, string num2) {
119
+ int m = num1.size(), n = num2.size();
120
+ vector<int > res(m + n);
121
+ for (int i = 0; i < n; ++i)
122
+ {
123
+ int b = num2[ n - 1 - i] - '0';
124
+ mul(num1, b, i, res);
104
125
}
126
+ string ans = "";
127
+ for (int v : res) ans.push_back(v + '0');
128
+ while (ans.size() > 1 && ans.back() == '0') ans.pop_back();
129
+ reverse(ans.begin(), ans.end());
105
130
return ans;
106
131
}
107
132
108
- private String add (String s1 , String s2 ) {
109
- int n1 = s1. length(), n2 = s2. length();
110
- StringBuilder res = new StringBuilder ();
111
- for (int i = 0 , carry = 0 ; i < Math . max(n1, n2) || carry > 0 ; ++ i) {
112
- int a = i < n1 ? (s1. charAt(n1 - i - 1 ) - ' 0' ) : 0 ;
113
- int b = i < n2 ? (s2. charAt(n2 - i - 1 ) - ' 0' ) : 0 ;
114
- int s = a + b + carry;
115
- carry = s / 10 ;
116
- res. append(s % 10 );
133
+ void mul(string A, int b, int i, vector<int>& res) {
134
+ for (int j = A.size() - 1, t = 0; j >= 0 || t > 0; --j)
135
+ {
136
+ if (j >= 0)
137
+ {
138
+ int a = A[j] - '0';
139
+ t += a * b;
140
+ }
141
+ res[i++] += t % 10 ;
142
+ if (res[i - 1 ] >= 10 )
143
+ {
144
+ res[i - 1] %= 10;
145
+ ++res[i];
146
+ }
147
+ t /= 10;
117
148
}
118
- return res. reverse(). toString();
119
149
}
150
+ };
151
+ ```
152
+
153
+ ### ** Go**
154
+
155
+ ``` go
156
+ func multiply (num1 string , num2 string ) string {
157
+ m , n := len (num1), len (num2)
158
+ res := make ([]int , m+n)
159
+ mul := func (b, i int ) {
160
+ for j , t := m-1 , 0 ; j >= 0 || t > 0 ; i, j = i+1 , j-1 {
161
+ if j >= 0 {
162
+ a := int (num1[j] - ' 0' )
163
+ t += a * b
164
+ }
165
+ res[i] += t % 10
166
+ if res[i] >= 10 {
167
+ res[i] %= 10
168
+ res[i+1 ]++
169
+ }
170
+ t /= 10
171
+ }
172
+ }
173
+ for i := 0 ; i < n; i++ {
174
+ b := num2[n-1 -i] - ' 0'
175
+ mul (int (b), i)
176
+ }
177
+ var ans []byte
178
+ for _ , v := range res {
179
+ ans = append (ans, byte (v+' 0' ))
180
+ }
181
+ for len (ans) > 1 && ans[len (ans)-1 ] == ' 0' {
182
+ ans = ans[:len (ans)-1 ]
183
+ }
184
+ for i , j := 0 , len (ans)-1 ; i < j; i, j = i+1 , j-1 {
185
+ ans[i], ans[j] = ans[j], ans[i]
186
+ }
187
+ return string (ans)
120
188
}
121
189
```
122
190
0 commit comments