MCPcopy Index your code
hub / github.com/doocs/leetcode / subStrings

Method subStrings

solution/0400-0499/0415.Add Strings/Solution.py:14–31  ·  view source on GitHub ↗
(self, num1: str, num2: str)

Source from the content-addressed store, hash-verified

12 return "".join(ans[::-1])
13
14 def subStrings(self, num1: str, num2: str) -> str:
15 m, n = len(num1), len(num2)
16 neg = m < n or (m == n and num1 < num2)
17 if neg:
18 num1, num2 = num2, num1
19 i, j = len(num1) - 1, len(num2) - 1
20 ans = []
21 c = 0
22 while i >= 0:
23 c = int(num1[i]) - c - (0 if j < 0 else int(num2[j]))
24 ans.append(str((c + 10) % 10))
25 c = 1 if c < 0 else 0
26 i, j = i - 1, j - 1
27 while len(ans) > 1 and ans[-1] == '0':
28 ans.pop()
29 if neg:
30 ans.append('-')
31 return ''.join(ans[::-1])

Callers

nothing calls this directly

Calls 3

appendMethod · 0.45
popMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected