gpt4 book ai didi

TypeError: 'ListNode' object has no attribute '__getitem__'(TypeError:‘ListNode’对象没有属性‘__getitem__’)

转载 作者:bug小助手 更新时间:2023-10-25 19:32:17 38 4
gpt4 key购买 nike



This is a problem i came across in leetcode.
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.

这是我在leetcode中遇到的问题。您将看到两个非空链接表,表示两个非负整数。数字以相反的顺序存储,并且它们的每个节点都包含一个数字。将这两个数字相加,然后以链表的形式返回总和。


You may assume the two numbers do not contain any leading zero, except the number 0 itself.

你可以假设这两个数字不包含任何前导零,除了数字0本身。


Input: l1 = [2,4,3], l2 = [5,6,4] Output: [7,0,8] Explanation: 342 + 465 = 807.

输入:L1=[2,4,3],L2=[5,6,4]输出:[7,0,8]解释:342+465=807。


this is the program ive written

这是我写的程序


class Solution(object):
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
r1=l1[::-1]m
r2=l2[::-1]
s1=[str(i) for i in r1]
digit1=int(''.join(s1))
s2=[str(j) for j in r2]
digit2=int(''.join(s2))
output=[ int(i) for i in str(digit1+digit2)]
output=output[::-1]
return output
print(Solution().addTwoNumbers([2, 4, 3], [5, 6, 4]))

I am getting the answer in pycharm.
but in leet code, I am getting this error..

我马上就能得到答案了。但是在leet代码中,我得到了这个错误。


Runtime Error

TypeError: 'ListNode' object has no attribute '__getitem__' r1=l1[::-1] Line 8 in addTwoNumbers (Solution.py) ret = Solution().addTwoNumbers(param_1, param_2) Line 42 in _driver (Solution.py) _driver() Line 52 in <module> (Solution.py)


What is the mistake here and how can i solve it?

这里的错误是什么?我如何才能解决它?


更多回答

You use a list in pycharm but a linked list is something different. The relevant code of ListNode is shown (commented out) when submitting your code to Leetcode.

你在pycharm中使用了列表,但链表是不同的。ListNode的相关代码在提交给Leetcode时显示(注释掉)。

So what should i do now?

那么我现在应该做什么呢?

First you need to understand what a linked list is. The Wikipedia article can be a starting point.

首先,您需要了解什么是链表。维基百科的文章可以作为一个起点。

优秀答案推荐
更多回答

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com