简体   繁体   English

Leetcode问题“加两个数字”中的错误output

[英]Wrong output in Leetcode problem "Add two numbers"

I'm trying to understand why I get different output when I run the code on leetcode and in VSC.我试图理解为什么我在 leetcode 和 VSC 上运行代码时会得到不同的 output。 I solved the problem "Add Two Numbers" in Python with the following code:我使用以下代码解决了 Python 中的“添加两个数字”问题:

from types import NoneType


class ListNode(object):
    def __init__(self, x):
        self.val = x
        self.next = None


class Solution(object):
    def addTwoNumbers(self, l1, l2):
        str_l1=""
        str_l2=""
        if type(l1.val) is int:
          my_l1=[l1.val]
        elif type(l1.val) is NoneType:
          my_l1=[0]
        else:
          my_l1=l1.val

        if type(l2.val) is int:
          my_l2=[l2.val]
        elif type(l2.val) is NoneType:
          my_l2=[0]
        else:
          my_l2=l2.val

        for i in range(len(my_l1)):
          str_l1+=str(my_l1[len(my_l1)-i-1])
        
        for i in range(len(my_l2)):
          str_l2+=str(my_l2[len(my_l2)-i-1])
        
        num_l1=int(str_l1)
        num_l2=int(str_l2)
        somma=num_l1+num_l2
        str_somma=str(somma)
        list_somma=[]
        for i in range(len(str_somma)):
          list_somma.append(int(str_somma[len(str_somma)-i-1]))
        sol=ListNode(list_somma)
        return sol

And this is the example:这是一个例子:

my_solution=Solution()
x=my_solution.addTwoNumbers(ListNode([2,4,3]),ListNode([5,6,4]))
print(x.val)

When I run the code in VSC I get the correct output [7,0,8], while when I run it in leetcode with the same input I get the wrong output [[7]].当我在 VSC 中运行代码时,我得到了正确的 output [7,0,8],而当我在 leetcode 中使用相同的输入运行它时,我得到了错误的 output [[7]]。 How is that possible?这怎么可能?

  1. The expected solution is a linked list, so a simple print(x.val) on one node doesn't display it very helpfully.预期的解决方案是一个链表,因此一个节点上的简单print(x.val)不会很有帮助地显示它。

You can make your own helper function to print the linked list, something like:您可以制作自己的助手 function 来打印链表,例如:

def print_ll(head):
    res = []
    n = head
    while n:
        res.append(n.val)
        n = n.next
    print(res)

If you use your solution and then do print_ll(x) , you get [[7, 0, 8]] .如果您使用您的解决方案然后执行print_ll(x) ,您将得到[[7, 0, 8]] Note, it's not [7, 0, 8] which is what you would expect.请注意,这不是您所期望的[7, 0, 8]

The reason is that the correct solution to the problem is a linked list with 3 nodes, each of which has an integer value.原因是问题的正确解决方案是一个有3个节点的链表,每个节点都有一个integer值。 But what your code gives is a single node, whose value is itself a list.但是您的代码给出的是单个节点,其值本身就是一个列表。

  1. Why does LeetCode show a value ( [[7]] ) which is different again?为什么 LeetCode 再次显示一个不同的值( [[7]] )?

Look at the input you give your code when you run in VSCode:查看在 VSCode 中运行时提供的代码输入:

x=my_solution.addTwoNumbers(ListNode([2,4,3]),ListNode([5,6,4]))

The inputs to the function there are not the same as it will get in LeetCode. function 的输入与 LeetCode 中的输入不同。 You are giving inputs which are single nodes with .val equal to a list.您提供的输入是.val等于列表的单个节点。 But on LeetCode, the inputs are linked lists with multiple nodes.但是在 LeetCode 上,输入是具有多个节点的链表。 So the result of this is not an accurate reflection of what happens when you run on LeetCode.所以这个结果并不能准确反映你在 LeetCode 上运行时发生的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM