https://leetcode-cn.com/problems/add-two-numbers/ def addition_of_two_numbers(l1, l2): len1 = len(l1) len2 = len(l2) if len1 < len2: l3 = l1 l1 = l2 l2 = l3 carry = 0 for ii in range(len1): if ii < len2: temp = l1[ii] + l2[ii] + carry else: temp = l1[ii] + carry if temp > 9: carry = 1 l1[ii] = temp - 10 else: carry = 0 l1[ii] = temp if carry > 0: l1.append(1) return l1 if __name__ == '__main__': l1 = [2,4,3] l2 = [5,6,4] print(addition_of_two_numbers(l1, l2)) l1 = [0] l2 = [0] print(addition_of_two_numbers(l1, l2)) l1 = [9, 9, 9, 9, 9, 9, 9] l2 = [9, 9, 9, 9] print(addition_of_two_numbers(l1, l2))
2021-03-17 leetcode刷题
最新推荐文章于 2024-08-14 17:00:26 发布