Simple Ruby Answer


  • 0
    S

    def add_strings(num1, num2)
    carry,result,num = 0,"",num
    (0...(num1.size-num2.size)).each {num2 = '0'+num2 } if num1.size>num2.size
    (0...(num2.size-num1.size)).each {num1 = '0'+num1 } if num2.size>num1.size
    (1..num1.size).each do |i|
    num = (num1[-i].to_i+num2[-i].to_i+carry)%10
    carry = (num1[-i].to_i+num2[-i].to_i+carry)/10
    result = num.to_s+result
    end
    result = '1'+result if(carry!=0)
    return result
    end


  • 0
    S
    This post is deleted!

Log in to reply
 

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