简体   繁体   English

为什么我会得到 keyError 呢?

[英]Why am I getting keyError on this?

The problem is: Given an array containing 0s and 1s, if you are allowed to replace no more than 'k' 0s with 1s, find the length of the longest contiguous subarray having all 1s.问题是:给定一个包含 0 和 1 的数组,如果允许用 1 替换不超过 'k' 个 0,请找出全为 1 的最长连续子数组的长度。

Input: Array=[0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1], k=2 Output: 6 Explanation: Replace the '0' at index 5 and 8 to have the longest contiguous subarray of 1s having length 6.输入:Array=[0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1], k=2 输出:6 解释:将索引 5 和 8 处的 '0' 替换为最长长度为 6 的 1 的连续子数组。

def length_of_longest_substring(arr, k):
    '''
    Create a hashmap that records the values of 0 and 1, initialize them to 0. Do a sliding 
    window.
    WHILE the frequency of 0 is greater than k, subtract arr[windowStart] from HM and then 
    increment 
    wS.
    Use the max function to record longest substring length. Return that.
    '''

    hm = {'0': '0', '1': '0'}
    (windowStart, longest) = (0, 0)
    for windowEnd in range(len(arr)):
        right = arr[windowEnd]
        hm[right] = hm.get(right, 0) + 1
        while hm["0"] > k:
            hm[arr[windowStart]] -= 1
            windowStart += 1
        longest = max(longest, windowEnd - windowStart + 1)
    return longest


def main():
    print(length_of_longest_substring([1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1], 2)) 
    #Return 6
    print(length_of_longest_substring([1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1], 3))
    #Return 9
main()

I am getting error with "while hm["0"] > k:" it says我收到错误“while hm [”0”] > k:”它说

File "main.py", line 12, in length_of_longest_substring
    while hm["0"] > k:
KeyError: 0

It works if I replace both starting indices with 0. I tried the .get function aswell.如果我将两个起始索引都替换为 0,它会起作用。我也尝试了 .get 函数。 I did hm.get("0"), same error.我做了 hm.get("0"),同样的错误。 I want the while loop to count the VALUES of 0. How can I achieve that?我希望 while 循环计算 0 的值。我怎样才能做到这一点? Thank you in advance, all is very much appreciated.提前感谢您,非常感谢您。

You are comparing integer and STR convert the STR to int您正在比较整数和 STR 将 STR 转换为 int

Note: I am not solving the problem but resolving the error as requested by the question.注意:我不是在解决问题,而是根据问题的要求解决错误。

def length_of_longest_substring(arr, k):
    '''
    Create a hashmap that records the values of 0 and 1, initialize them to 0. Do a sliding 
    window.
    WHILE the frequency of 0 is greater than k, subtract arr[windowStart] from HM and then 
    increment 
    wS.
    Use the max function to record longest substring length. Return that.
    '''

    hm = {'0': '0', '1': '0'}
    (windowStart, longest) = (0, 0)
    for windowEnd in range(len(arr)):
        right = arr[windowEnd]
        hm[right] = hm.get(right, 0) + 1
        while (((int)(hm["0"])) > k):
            hm[arr[windowStart]] -= 1
            windowStart += 1
        longest = max(longest, windowEnd - windowStart + 1)
    return longest


def main():
    print(length_of_longest_substring([1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1], 2)) 
    #Return 6
    print(length_of_longest_substring([1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1], 3))
    #Return 9
main()

I think you got the logic wrong in your for loop.我认为您的for循环中的逻辑错误。 I have modified it and the error got fixed.我已经修改它并且错误得到了修复。

# While the frequency of 0 is greater than k, subtract arr[windowStart] from hm and then increment windowStart.
if hm[0] > k:
    hm[arr[windowStart]] -= 1
    windowStart += 1

# Record longest substring length.
longest = max(longest, windowEnd - windowStart + 1)

# Increment the frequency of arr[windowEnd] in hm.
hm[arr[windowEnd]] += 1

Output:输出:

6
10

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM