微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决在使用递归方法反转字符串时,在 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

,

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。