-
Notifications
You must be signed in to change notification settings - Fork 115
/
0415-AddStrings.cs
50 lines (42 loc) · 1.34 KB
/
0415-AddStrings.cs
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
//-----------------------------------------------------------------------------
// Runtime: 84ms
// Memory Usage: 25.6 MB
// Link: https://leetcode.com/submissions/detail/352313138/
//-----------------------------------------------------------------------------
using System.Linq;
using System.Text;
namespace LeetCode
{
public class _0415_AddStrings
{
public string AddStrings(string num1, string num2)
{
var sb = new StringBuilder();
int i = num1.Length - 1, j = num2.Length - 1, carry = 0;
while (i >= 0 && j >= 0)
{
var value = num1[i--] - '0' + num2[j--] - '0' + carry;
var digit = value % 10;
carry = value / 10;
sb.Append(digit);
}
while (i >= 0)
{
var value = num1[i--] - '0' + carry;
var digit = value % 10;
carry = value / 10;
sb.Append(digit);
}
while (j >= 0)
{
var value = num2[j--] - '0' + carry;
var digit = value % 10;
carry = value / 10;
sb.Append(digit);
}
if (carry != 0)
sb.Append(carry);
return new string(sb.ToString().Reverse().ToArray());
}
}
}