"Leetcode" 415. Add Strings "E"

Source: Internet
Author: User

Given two non-negative numbers NUM1 and num2 represented as String, return the sum of NUM1 and num2.

Note:the length of both NUM1 and num2 is < 5100. Both Num1 and num2 contains only digits 0-9. Both NUM1 and num2 does not contain any leading zero. You are must not with any built-in BigInteger library or convert the inputs to integer directly.

Subscribe to the which companies asked this question

This is a straightforward one, and the last line of comments is a handy python integer type.
The total feeling program where write a bit ugly ~ ~ ~ ~



Class Solution (object):
    def addstrings (self, NUM1, num2):

        l1 = Len (num1)
        L2 = Len (num2)

        lmin = min (l1,l2
        Lmax = max (L1,L2)

        res = [0] * (lmax+1)
        #print res for

        i in Xrange (1,lmin+1):
            res[-i] = Int (num1[-i)) + int (num2[-i])
            #print i,res

        if L1 > L2: For
            i in Xrange (lmin+1,lmax+1):
                res[-i] = Int (num1[-i)) 
  else: For
            i-xrange (lmin+1,lmax+1):
                res[-i] = Int (num2[-i)) for

        I in Xrange (1,lmax+1):
            If Res [I] > 9:
                res[-i] =
                Res[-i-1] + = 1
        rres = ' for

        i in res:
            rres + = str (i)
        if rres[0] = = ' 0 ':
            rres = rres[1:] return
        rres


        #return str (int (NUM1) + int (num2))


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.