Skip to content

Latest commit

 

History

History
31 lines (25 loc) · 622 Bytes

李戈-20180820.md

File metadata and controls

31 lines (25 loc) · 622 Bytes

LeetCode

题目: Add Two Numbers

解题:

def add_two_numbers(l1, l2)
 add = 0
 arr=[]

 while !l1.nil? || !l2.nil? || add > 0
   val1, val2 = 0,0
   if l1 != nil
     val1, l1 = l1.val, l1.next
   end
   if l2 != nil
     val2, l2 = l2.val, l2.next
   end
   val = val1 + val2 + add
   add = val / 10
   arr << val % 10
 end

 arr
end

解题思路: 就是简单的小学加法运算,用代码表示出来,注意的是这个加法是倒过来的,依旧是逢十进一,下一次 运算加上进位数。