```


  • 0
    L
            
            int len1 = num1.length();
            int len2 = num2.length();
            int carry=0, i=0, j=0;
            
            StringBuilder sb = new StringBuilder();
            
            for(i=len1-1,j=len2-1;i>=0 && j>=0;i--,j--){
                
                int res = Character.getNumericValue(num1.charAt(i)) + Character.getNumericValue(num2.charAt(j)) +carry;
                
                if(res<=9) {
                    sb.append(res);
                    carry=0;
                    continue;
                }else{
                    sb.append(res%10);
                    carry =1;
                }
            }
            
            while(i>=0){
                int res = Character.getNumericValue(num1.charAt(i))+carry;
                sb.append(res%10);
                carry= res>=10?1:0;
                i--;
            }
            while(j>=0){
                int res = Character.getNumericValue(num2.charAt(j))+carry;
                sb.append(res%10);
                carry= res>=10?1:0;
                j--;
            }
            if(carry>0) 
                sb.append(carry);
            
            return sb.reverse().toString();
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.