https://leetcode.com/problems/add-strings/ package com.company; import java.util.LinkedList; import java.util.List; import java.util.Random; public class Main { public String addStrings(String num1, String num2) { StringBuilder sb = new StringBuilder(); if (num1.length() > num2.length()) { String tmp = num1; num1 = num2; num2 = tmp; } int i = num1.length() - 1; int j = num2.length() - 1; int carry = 0; int ival = 0; for (; i>=0 && j >= 0; i--,j--) { ival = carry + num1.charAt(i) - '0' + num2.charAt(j) - '0'; if (ival > 9) { carry = ival / 10; ival %= 10; } else { carry = 0; } sb.append((char)('0' + ival)); } // num2 is always longer than num1 while (j >= 0) { ival = carry + num2.charAt(j) - '0'; if (ival > 9) { carry = ival / 10; ival %= 10; } else { carry = 0; } sb.append((char)('0' + ival)); j--; } // At first, it's missed if (carry > 0) { sb.append((char)('0' + carry)); } return sb.reverse().toString(); } public static void main(String[] args) { // write your code here System.out.println("Hello"); String num1 = "1"; String num2 = "9"; Main obj = new Main(); String ret = obj.addStrings(num1, num2); System.out.printf("ret: %s ", ret); } }