-
-
Notifications
You must be signed in to change notification settings - Fork 608
/
Copy pathAddStrings.java
35 lines (32 loc) · 855 Bytes
/
AddStrings.java
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
package problems.easy;
/**
* Created by sherxon on 1/10/17.
*/
/**
* Problem: Given two non-negative integers a and b (length of a and b < 5100) represented as string, return the sum of a and b.
*/
public class AddStrings {
/**
* Start adding from the least significant digit.
*/
static String addStrings(String a, String b) {
int i=a.length()-1;
int j=b.length()-1;
int carry=0;
StringBuilder sb= new StringBuilder();
while (i>=0 || j>=0){
int aa=0;
int bb=0;
if(i>=0)
aa=a.charAt(i--)-'0';
if(j>=0)
bb=b.charAt(j--)-'0';
int sum=(aa+bb+carry);
sb.append(sum%10);
carry=sum/10;
}
sb=sb.reverse();
if(carry>0)sb.insert(0, carry);
return sb.toString();
}
}