当前位置: 首页>>代码示例>>Python>>正文


Python Solution.addTwoNumbers方法代码示例

本文整理汇总了Python中solution.Solution.addTwoNumbers方法的典型用法代码示例。如果您正苦于以下问题:Python Solution.addTwoNumbers方法的具体用法?Python Solution.addTwoNumbers怎么用?Python Solution.addTwoNumbers使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在solution.Solution的用法示例。


在下文中一共展示了Solution.addTwoNumbers方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TestCase

# 需要导入模块: from solution import Solution [as 别名]
# 或者: from solution.Solution import addTwoNumbers [as 别名]
class TestCase(unittest.TestCase):
    def setUp(self):
        self.solution = Solution()

    def assertListNodeEqual(self, l1, l2):
        if l1 is None and l2 is None:
            return
        self.assertEqual(l1.val, l2.val)
        self.assertListNodeEqual(l1.next, l2.next)

    def test_factory(self):
        l = ListNodeFactory.create(1, 2, 3)
        self.assertEqual(l.val, 1)
        self.assertEqual(l.next.val, 2)
        self.assertEqual(l.next.next.val, 3)

    def test_solution1(self):
        l1 = ListNodeFactory.create(2, 4, 3)
        l2 = ListNodeFactory.create(5, 6, 4)
        answer = self.solution.addTwoNumbers(l1, l2)
        self.assertListNodeEqual(answer, ListNodeFactory.create(7, 0, 8))

    def test_solution2(self):
        l1 = ListNodeFactory.create(1, 2, 3, 4, 5)
        l2 = ListNodeFactory.create(9, 8, 7, 6, 5, 4)
        answer = self.solution.addTwoNumbers(l1, l2)
        self.assertListNodeEqual(answer, ListNodeFactory.create(0, 1, 1, 1, 1, 5))

    def test_solution2(self):
        l1 = ListNodeFactory.create(5)
        l2 = ListNodeFactory.create(5)
        answer = self.solution.addTwoNumbers(l1, l2)
        self.assertListNodeEqual(answer, ListNodeFactory.create(0, 1))
开发者ID:romain-li,项目名称:leetcode,代码行数:35,代码来源:test.py

示例2: test_2

# 需要导入模块: from solution import Solution [as 别名]
# 或者: from solution.Solution import addTwoNumbers [as 别名]
def test_2():
    """
    7 + 6 = 13
    """
    sol = Solution()
    l1 = ListNode(7)
    l2 = ListNode(6)
    l = sol.addTwoNumbers(l1, l2)
    p = l
    assert p.val == 3
    p = p.next
    assert p.val == 1
    p = p.next
    assert not p
开发者ID:misaka-10032,项目名称:leetcode,代码行数:16,代码来源:test.py

示例3: test_3

# 需要导入模块: from solution import Solution [as 别名]
# 或者: from solution.Solution import addTwoNumbers [as 别名]
def test_3():
    """
    8 + 94 = 102
    """
    sol = Solution()
    l1 = ListNode(8)
    l2 = ListNode(9)
    l2 = insert_ll(l2, 4)
    l = sol.addTwoNumbers(l1, l2)
    p = l
    assert p.val == 2
    p = p.next
    assert p.val == 0
    p = p.next
    assert p.val == 1
    p = p.next
    assert not p
开发者ID:misaka-10032,项目名称:leetcode,代码行数:19,代码来源:test.py

示例4: test_1

# 需要导入模块: from solution import Solution [as 别名]
# 或者: from solution.Solution import addTwoNumbers [as 别名]
def test_1():
    """
    243 + 564 = 807
    """
    sol = Solution()
    l1 = ListNode(2)
    l1 = insert_ll(l1, 4)
    l1 = insert_ll(l1, 3)
    l2 = ListNode(5)
    l2 = insert_ll(l2, 6)
    l2 = insert_ll(l2, 4)
    l = sol.addTwoNumbers(l1, l2)
    p = l
    assert p.val == 7
    p = p.next
    assert p.val == 0
    p = p.next
    assert p.val == 8
    p = p.next
    assert not p
开发者ID:misaka-10032,项目名称:leetcode,代码行数:22,代码来源:test.py

示例5: mkListNode

# 需要导入模块: from solution import Solution [as 别名]
# 或者: from solution.Solution import addTwoNumbers [as 别名]
def mkListNode(nums):
    """
    :type: nums: List
    :rtype: ListNode
    """
    head = lnp = ListNode(0)
    for i in range(len(nums)):
        lnp.next = ListNode(nums[i])
        lnp = lnp.next
    return head.next


def lsListNode(head):
    """
    :type: head: ListNode
    :rtype: List
    """
    lnp = head
    rList = []
    while lnp != None:
        rList.append(lnp.val)
        lnp = lnp.next
    return rList


inpt1 = mkListNode([2, 4, 3])
inpt2 = mkListNode([5, 6, 4])
sol = Solution()
res = sol.addTwoNumbers(inpt1, inpt2)
print(lsListNode(res))
开发者ID:zhlinh,项目名称:leetcode,代码行数:32,代码来源:test.py


注:本文中的solution.Solution.addTwoNumbers方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。