MCPcopy
hub / github.com/doocs/leetcode / addStrings

Method addStrings

solution/0400-0499/0415.Add Strings/Solution.java:2–13  ·  view source on GitHub ↗
(String num1, String num2)

Source from the content-addressed store, hash-verified

1class Solution {
2 public String addStrings(String num1, String num2) {
3 int i = num1.length() - 1, j = num2.length() - 1;
4 StringBuilder ans = new StringBuilder();
5 for (int c = 0; i >= 0 || j >= 0 || c > 0; --i, --j) {
6 int a = i < 0 ? 0 : num1.charAt(i) - '0';
7 int b = j < 0 ? 0 : num2.charAt(j) - '0';
8 c += a + b;
9 ans.append(c % 10);
10 c /= 10;
11 }
12 return ans.reverse().toString();
13 }
14
15 public String subStrings(String num1, String num2) {
16 int m = num1.length(), n = num2.length();

Callers

nothing calls this directly

Calls 4

lengthMethod · 0.80
appendMethod · 0.45
toStringMethod · 0.45
reverseMethod · 0.45

Tested by

no test coverage detected