鱼C论坛

 找回密码
 立即注册
查看: 316|回复: 2

递归代码如何修改

[复制链接]
发表于 2024-11-4 16:56:50 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. from functools import lru_cache
  2. @lru_cache
  3. def removeInvalidParentheses(self, s):
  4.     if len(s) == 1 and s not in "()":
  5.         return [s]
  6.     if len(s) < 2:
  7.         return [""]
  8.     res = set()
  9.     if s[0] == "(" and s[-1] == ")":
  10.         res |= set([f"({i})" for i in removeInvalidParentheses(s[1:-1])])
  11.     for i in range(1, len(s)):
  12.         a, b = removeInvalidParentheses(s[:i]), removeInvalidParentheses(s[i:])
  13.         res |= {i+j for i in a for j in b}
  14.         print(res)
  15.     p = len(max(res, key=len))
  16.     return [i for i in res if len(i) == p]
  17. removeInvalidParentheses(s)
复制代码


代码报错如下,如何修改
  1. TypeError                                 Traceback (most recent call last)
  2. <ipython-input-13-806560d14b0b> in <cell line: 17>()
  3.      15     p = len(max(res, key=len))
  4.      16     return [i for i in res if len(i) == p]
  5. ---> 17 removeInvalidParentheses()

  6. TypeError: removeInvalidParentheses() missing 2 required positional arguments: 'self' and 's'
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-11-4 17:00:09 | 显示全部楼层
你的代码中有几个问题需要修正:

1. **函数定义问题**:`removeInvalidParentheses` 函数定义中有一个 `self` 参数,但在调用时没有传递。这个函数看起来像是一个类的方法,但你在调用时没有实例化类。

2. **递归调用问题**:在递归调用中,`removeInvalidParentheses(s)` 应该传递一个参数 `s`,而不是空调用。

3. **逻辑问题**:在 `if s == "(" and s == ")"` 这行逻辑上有问题,因为一个字符串不可能同时等于 `"("` 和 `")"`。

4. **返回值问题**:函数没有返回任何值。

以下是修正后的代码示例:

  1. from functools import lru_cache

  2. @lru_cache
  3. def removeInvalidParentheses(s):
  4.     if len(s) == 1 and s not in "()":
  5.         return {s}
  6.     if len(s) < 2:
  7.         return {""}
  8.    
  9.     res = set()
  10.     if s == "(" or s == ")":
  11.         res |= set([f"({i})" for i in removeInvalidParentheses(s)])
  12.    
  13.     for i in range(len(s)):
  14.         if s[i] in "()":
  15.             new_s = s[:i] + s[i+1:]
  16.             res |= removeInvalidParentheses(new_s)
  17.    
  18.     # 过滤出最长的结果
  19.     max_len = max(len(x) for x in res)
  20.     return {x for x in res if len(x) == max_len}

  21. # 示例调用
  22. s = "()())()"
  23. result = removeInvalidParentheses(s)
  24. print(result)
复制代码


在这个修正后的代码中:

- 移除了 `self` 参数,因为这个函数不需要类实例。
- 修正了递归调用,确保传递了参数 `s`。
- 修正了逻辑错误,确保条件判断正确。
- 添加了返回值,确保函数返回一个集合。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-11-4 17:23:23 | 显示全部楼层
本帖最后由 jackz007 于 2024-11-4 19:01 编辑
  1. def removeInvalidParentheses(self, s):
复制代码

          为何会有 self 参数,难道这是一个类方法?形式参数 self 在函数内一次都没有被使用!
          如果 removeInvalidParenthese() 是类方法的话,就不能这样来调用:
  1. removeInvalidParentheses(s)
复制代码

          应该这样调用:
  1. self . removeInvalidParentheses(s)
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-18 16:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表