Problem
My Answer
- 1 hour limit
- no search on internet
My code
class Solution {
public String addStrings(String num1, String num2) {
StringBuilder sb = new StringBuilder();
int num1Leng = num1.length() - 1;
int num2Leng = num2.length() - 1;
int round = 0;
for (int i = Math.max(num1.length(), num2.length()) - 1; i > -1; --i) {
if (num1Leng < 0) {
sb.append(String.valueOf(((num2.charAt(num2Leng) - 48) + round)%10));
if ((((num2.charAt(num2Leng) - 48) + round)/10) > 0) {
round=1;
}else{
round=0;
}
} else if (num2Leng < 0) {
sb.append(String.valueOf(((num1.charAt(num1Leng) - 48) + round)%10));
if ((((num1.charAt(num1Leng) - 48) + round)/10) > 0) {
round=1;
}else{
round=0;
}
} else {
sb.append(String.valueOf((((num1.charAt(num1Leng) - 48 + num2.charAt(num2Leng) - 48+round) % 10))));
if ((((num1.charAt(num1Leng) - 48 + num2.charAt(num2Leng) - 48)+round) / 10) > 0) {
round=1;
}else{
round=0;
}
}
--num1Leng;
--num2Leng;
}
if(round != 0){
sb.append("1");
}
return sb.reverse().toString();
}
}
- Time complexity : $O(max(n,m))$
- Space complexity : $O(1)$
My result
- Runtime: 4 ms, faster than 32.72% of Java online submissions for Add Strings.
- Memory Usage: 40.8 MB, less than 5.11% of Java online submissions for Add Strings.
Best Answer
Search from internet and modify
class Solution {
public String addStrings(String num1, String num2) {
StringBuilder sb = new StringBuilder();
int index1 = num1.length() - 1;
int index2 = num2.length() - 1;
int round = 0;
//if both index are -1, stop loop. otherwise, keep loop
//start from the end of the list for addtion
while (index1 >= 0 || index2 >= 0) {
int temp1 = index1 < 0 ? 0 : num1.charAt(index1) - '0';//if index is -1, put 0 if not, put number
int temp2 = index2 < 0 ? 0 : num2.charAt(index2) - '0';//if index is -1, put 0 if not, put number
sb.insert(0, (temp1 + temp2 + round) % 10);//insert number without carry at the most left
round = (temp1 + temp2 + round) / 10;//save carried number for next loop
--index1;
--index2;
}
if(round != 0){//If last addtion has carry, add them
sb.insert(0,"1");
}
return sb.toString();//stringBuilder to String and return
}
}
- Time complexity : $O(max(n,m))$
- Space complexity : $O(1)$
Reflect on
- Basic algorithm and using stringBuilder was correct. However, my code is not optimizated.
- ternary conditional operator is only possible to use higher than Java 8
- Code readability is much better