Simple Java Solution 26 ms


  • 0
    D

    public class Solution {
    public String addStrings(String num1, String num2) {

        if((num1.length()==0) && (num2.length()==0)) return null;
        StringBuffer sb = new StringBuffer();
        int sum=0;
        int carry=0;
        int i,j;
        for( i=num2.length()-1,j=num1.length()-1;i>=0 && j>=0 ;i--,j--)
        {sum=0;
          
            sum+=(num2.charAt(i)-'0')+(num1.charAt(j)-'0')+carry;
            
            sb.append(sum%10);
            carry=sum/10;
        }
        while(i>=0)
        {
            sum=(num2.charAt(i)-'0')+carry;
            sb.append(sum%10);
            carry=sum/10;
            i--;
            
        }
    
           while(j>=0)
        {
            sum=(num1.charAt(j)-'0')+carry;
            sb.append(sum%10);
            carry=sum/10;
            j--;
            
        }
             System.out.print("c"+carry);
        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.