- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path415_Add_Strings.java
More file actions
Latest commit
15 lines (15 loc) · 645 Bytes
/
Copy path415_Add_Strings.java
File metadata and controls
15 lines (15 loc) · 645 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
publicclassSolution {
publicStringaddStrings(Stringnum1, Stringnum2) {
StringBuildersb = newStringBuilder();
intcarry = 0;
// condition is great
// https://leetcode.com/problems/add-strings/discuss/90436/Straightforward-Java-8-main-lines-25ms
for(inti = num1.length() - 1, j = num2.length() - 1; i >= 0 || j >= 0 || carry == 1; i--, j--){
intx = i < 0 ? 0 : num1.charAt(i) - '0';
inty = j < 0 ? 0 : num2.charAt(j) - '0';
sb.append((x + y + carry) % 10);
carry = (x + y + carry) / 10;
}
returnsb.reverse().toString();
}
}