-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddStrings.php
More file actions
54 lines (52 loc) · 1.43 KB
/
Copy pathAddStrings.php
File metadata and controls
54 lines (52 loc) · 1.43 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
namespace App;
/**
* Add Strings
*
* Given two non-negative integers, num1 and num2 represented as string, return the sum of num1 and num2 as a string.
* You must solve the problem without using any built-in library for handling large integers (such as BigInteger). You
* must also not convert the inputs to integers directly.
*
* Example 1:
* Input: num1 = "11", num2 = "123"
* Output: "134"
*
* Example 2:
* Input: num1 = "456", num2 = "77"
* Output: "533"
*
* https://leetcode.com/problems/add-strings
*/
class AddStrings
{
/**
* @param string $num1
* @param string $num2
* @return string
*/
public function addStrings(string $num1, string $num2): string
{
$num1Index = strlen($num1) - 1;
$num2Index = strlen($num2) - 1;
$remainder = 0;
$result = '';
while ($num1Index >= 0 || $num2Index >= 0) {
$digit1 = 0;
if ($num1Index >= 0) {
$digit1 = (int) $num1[$num1Index];
}
$digit2 = 0;
if ($num2Index >= 0) {
$digit2 = (int) $num2[$num2Index];
}
$result = (($digit1 + $digit2 + $remainder) % 10) . $result;
$remainder = (int) (($digit1 + $digit2 + $remainder) / 10);
$num1Index--;
$num2Index--;
}
if ($remainder > 0) {
$result = $remainder . $result;
}
return $result;
}
}