Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python-查找字符串中最长的非重复子字符串_Python - Fatal编程技术网

python-查找字符串中最长的非重复子字符串

python-查找字符串中最长的非重复子字符串,python,Python,Python 2.7 从elif中的索引错误中获取堆栈 查找字符串中最长的子字符串(仅限非重复字符) 返回长度 from pythonds.basic import Stack def longest_nonrepeating_len(s): """find longest non-repeating substring and return its length""" if len(s) < 1: return [] longest_substr

Python 2.7 从elif中的索引错误中获取堆栈 查找字符串中最长的子字符串(仅限非重复字符) 返回长度

from pythonds.basic import Stack
def longest_nonrepeating_len(s):
    """find longest non-repeating substring and return its length"""
    if len(s) < 1:
        return []

    longest_substring = 0
    max_long_substring = 0
    stack = Stack()
    stack.push(s[0])

    for char in range(0,len(s)):
        if stack.isEmpty():
            stack.push(s[char])
            longest_substring = 1
            max_long_substring = 1
            continue

        if s[char] != stack.pop():
            longest_substring += 1
            stack.push(s[char])
            max_long_substring = longest_substring
        elif s[char] == stack.pop():
            longest_substring = 0
            stack.push(s[char])

    return max_long_substring
来自pythonds.basic导入堆栈的

def最长非重复长度:
“”“查找最长的非重复子字符串并返回其长度”“”
如果长度小于1:
返回[]
最长的子字符串=0
最大长度子字符串=0
stack=stack()
stack.push(s[0])
对于范围(0,len)中的字符:
如果stack.isEmpty():
stack.push(s[char])
最长的子字符串=1
最大长度子串=1
持续
如果s[char]!=stack.pop():
最长子串+=1
stack.push(s[char])
最大长子串=最长子串
elif s[char]==stack.pop():
最长的子字符串=0
stack.push(s[char])
返回最大长度子字符串
错误:

Traceback (most recent call last):
  File "python", line 1, in <module>
  File "python", line 23, in longest_nonrepeating_len
IndexError: pop from empty list

>>> longest_nonrepeating_len("abccd")
3
>>> longest_nonrepeating_len("ffff")
1
回溯(最近一次呼叫最后一次):
文件“python”,第1行,在
文件“python”,第23行,最长不重复
索引器:从空列表中弹出
>>>最长非重复长度(“abccd”)
3.
>>>最长不重复长度(“ffff”)
1.

stack
只有一个元素时会发生这种情况,因为您一行调用stack.pop()两次。我想你的意思是:

    pop = stack.pop()
    if s[char] != pop:
        longest_substring += 1
        stack.push(s[char])
        max_long_substring = longest_substring
    elif s[char] == pop:
        longest_substring = 0
        stack.push(s[char])

然而,我认为您的代码逻辑仍然存在缺陷:在更新它之前,您是否应该检查
最长的子字符串
是否确实比先前的
最长的子字符串
值长?

堆栈
只有一个元素时会发生这种情况,因为您连续调用了两次stack.pop()。我想你的意思是:

    pop = stack.pop()
    if s[char] != pop:
        longest_substring += 1
        stack.push(s[char])
        max_long_substring = longest_substring
    elif s[char] == pop:
        longest_substring = 0
        stack.push(s[char])

然而,我认为您的代码逻辑仍然存在缺陷:在更新之前,您是否应该检查
最长的子字符串
是否实际比以前的
最长的子字符串
值长?

您所说的“不重复”是什么意思?字符串中最长的非重复子字符串始终是整个字符串本身。但如果您询问某个特定错误,请完整显示该错误。“abcdeef”-abcde将是非重复的,f.也是
abcabc
非重复的?您只是在寻找没有相同字符运行的子字符串吗?您所说的“不重复”是什么意思?字符串中最长的不重复子字符串始终是整个字符串本身。但如果您询问某个特定错误,请完整显示该错误。“abcdeef”-abcde将是不重复的,与f一样,
abcabc
是否不重复?你只是在寻找没有相同字符运行的子字符串吗?是的,我怀疑我有其他问题…我只是被堆栈错误卡住了。是的,我怀疑我有其他问题…我只是被堆栈错误卡住了。