在使用递归方法反转字符串时,在 Python3 下的 Leetcode 中执行以下代码时出错

问题描述

我正在做关于 Python 基础知识的 leetcode 问题。我在 leetcode 上收到如下错误代码

TypeError: reverseString() takes 1 positional argument but 2 were given ret = Solution().reverseString(param_1) Line 28 in _driver (Solution.py) _driver()
 class Solution:
        def reverseString(s: List[str]) -> None:
            if len(s)==0:
                return s
            else:
                return Solution.reverseString(s[1:])+s[0]

解决方法

from sqlalchemy.ext.hybrid import hybrid_property class Parent(Base): ... children = relationship("Child",order_by="Child.age") @hybrid_property def youngest_child(self): """ children are already sorted by their age in the relationship. This offers an advantage in performance when children are frequently accessed through their sorted property. """ return self.children[0] if self.children # - - - - - - - - - - - - - - - - - - # Another approach – without pre-defined ordering class Parent(Base): ... # Don't order the children children_no_order = relationship("Child") @hybrid_property def youngest_child_no_order(self): """ children are not automatically sorted by their age. Do so with a subquery hybrid. """ query = Child.query\ .filter(Child.parent_id == self.id) .order_by(Child.age)\ .first() # I chose to write this query in this format bc it helps with debugging # You could just as easily return the query without saving its state... return query 是一个类方法,但您没有将 reverseString 参数定义为第一个参数。您应该将其作为第一个参数,或者使用 @staticmethod decorator。这在现有的帖子中有更好的解释:TypeError: method() takes 1 positional argument but 2 were given

,

您需要提供一个参数而不是两个。