python - CS50 面试问题 - 查找最长的子串递归之谜 - 为什么测试用例 #4 不起作用?
问题描述
问题:
给定一个字符串,找出最长的不重复字符的子串的长度。例如,在字符串“abcabcbb”中,没有重复字符的最长子串的长度为 3(“abc”等)。
问题描述:
嗨,我为此使用了 Python 和递归解决方案。除了 TC4,每个测试用例都按预期工作,我不知道为什么。看起来当找到最终的子字符串(“wekf”)时,它会停留在同一个迭代中并再次以原始(!)字符串开始。它怎么还能保留原来的字符串?输出是完全出乎意料的“pwekf”......请帮助我解决这个大谜团。
代码:
def find_longest_substring(string, longest=0):
current_longest_string = ""
for char, i in zip(string, range(len(string))):
if char not in current_longest_string:
current_longest_string = f'{current_longest_string}{char}'
# if character is already in current_longest_string, count len() and
# start looking again from last spot
else:
longest = len(current_longest_string) \
if len(current_longest_string) > longest else longest
find_longest_substring(string[i:], longest)
longest = len(current_longest_string)
return longest, current_longest_string
# TC1
print(find_longest_substring("abcabcbb"))
# TC2
print(find_longest_substring("bbbbb"))
# TC3
print(find_longest_substring("abcdef"))
# TC4
print(find_longest_substring("pwwekf"))
# TC5
print(find_longest_substring(""))
固定的:
def find_longest_substring(string, longest=0):
current_longest_string = ""
for char, i in zip(string, range(len(string))):
if char not in current_longest_string:
current_longest_string = f'{current_longest_string}{char}'
# if character is already in current_longest_string, count len() and
# start looking again from last spot
else:
longest = len(current_longest_string) \
if len(current_longest_string) > longest else longest
longest_new = find_longest_substring(string[i:], longest)
if longest_new > longest:
return longest_new
longest = len(current_longest_string)
return longest
输出:
(3, 'abc')
(1, 'b')
(6, 'abcdef')
(5, 'pwekf')
(0, '')
解决方案
您可能会注意到这是唯一一个从一开始就没有正确结果的测试用例。您可能还会注意到您的函数是贪婪的,并且永远无法退出错误的解决方案。递归什么都不做,递归调用的输出在任何地方都没有使用。
在 else 路径中,您必须:
- 进行递归调用
- 检查解决方案是否比您当前的解决方案更好
- 如果是,更换它
- 跳出 for 循环
推荐阅读
- python - 在 Django 中返回位置标头
- javascript - 将 Combobox 绑定到 VueJs 中的数组
- android - 如何以反应原生形式添加 ReCaptcha-v2?
- c# - 如何使用 C# 在 Json 上的特定级别添加新属性
- neo4j - Node中找不到值时的回退遍历方法
- azure - PowerBI 访问 Azure SQL 数据库(PaaS)
- scikit-learn - resnet 实现 pytorch 的准确率和召回率是一样的
- python - AttributeError:“ec2.ServiceResource”对象没有属性“send_command”
- python - Python根据另一个数据框中的多个条件添加新列值
- excel - 根据新文件的列名组合两个excel文件