forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.php
29 lines (25 loc) · 836 Bytes
/
Solution.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution {
/**
* @param string $num1
* @param string $num2
* @return string
*/
function multiply($num1, $num2) {
$length1 = strlen($num1);
$length2 = strlen($num2);
$product = array_fill(0, $length1 + $length2, 0);
for ($i = $length1 - 1; $i >= 0; $i--) {
for ($j = $length2 - 1; $j >= 0; $j--) {
$digit1 = intval($num1[$i]);
$digit2 = intval($num2[$j]);
$temp = $digit1 * $digit2 + $product[$i + $j + 1];
$product[$i + $j + 1] = $temp % 10;
$carry = intval($temp / 10);
$product[$i + $j] += $carry;
}
}
$result = implode('', $product);
$result = ltrim($result, '0');
return $result === '' ? '0' : $result;
}
}