带有内部while循环的forloop中的python列表索引错误

问题描述 投票:0回答:1

这是我的并发代码:

from collections import Counter
class Solution:
    def minWindow(self, s: str, t: str) -> str:

        left = 0
        right = float("inf")
        ref = Counter(t)
        necessary_count = sum(ref.values())
        curr_count = 0
        curr_dict = Counter()
        slow = 0
        for i in range(0,len(s)):
            c=s[i]
            if c in ref:
                curr_dict[c]+=1
                if curr_dict[c]<=ref[c]:
                    curr_count+=1
            while curr_count == necessary_count:
                if (i-slow)<(right-left):
                    left,right = slow,i
                s = s[slow]
                if s in ref:
                    curr_dict[s]-=1
                if curr_dict[s]<ref[s]:
                    curr_count-=1
                slow+=1
        if right == float("inf"): return ''
        return s[left:right+1]


示例测试用例:

Input: S = "ADOBECODEBANC", T = "ABC"
Expected Output: "BANC"
Actual Output:""

当前,我在声明c=s[i]时收到索引错误。当我通过调试器时,它会在我完成while循环的程序的第一次遇到后立即引发错误。为什么我违反了s (0<=i<len(s))的索引,而forloop仍在那些约束之内?它与我的while循环有关系吗?

python algorithm list debugging indexing
1个回答
1
投票

确实与您的while循环有关:

© www.soinside.com 2019 - 2024. All rights reserved.