簡體   English   中英

Leetcode 28 問題 - 為什么我的 strStr() 失敗了?

[英]Leetcode 28 Question - Why is my strStr() failing?

我正在嘗試解決這個 leetcode 問題: https://leetcode.com/explore/interview/card/top-interview-questions-easy/127/strings/885/

這個想法是您編寫的代碼可以在字符串中找到 substring。 為了練習起見,我正在(或至少曾經)試圖以最有效的方式做到這一點。 我認為它可以在一個“for”循環中完成(希望只需要 O(n) 時間)

這是我的代碼

class Solution:
    def strStr(self, haystack: str, needle: str) -> int:
        needle = list(needle)
        haystack = list(haystack)
        goodPos = None
        goodCounter = 0
        if len(needle) == 0:
            return 0
        if len(haystack) == 0 or len(needle) > len(haystack):
            return -1
        for key, value in enumerate(haystack):
            if needle[goodCounter] != value:
                print(f"{needle[goodCounter]} does not match {value}, resetting")
                goodPos = None
                goodCounter = 0
            if needle[goodCounter] == value:
                print(f"We have a match at {value}")
                if goodCounter == 0:
                    print(f"We are setting the key at {key}")
                    goodPos = key
                goodCounter += 1
            if len(needle) == goodCounter:
                return goodPos
        print("Finished for loop")
        print(f"goodPos at {goodPos}")
        print(f"goodCounter at {goodCounter}")
        #For situations where we didnt caught it
        if goodCounter == len(haystack):
            return len(haystack)
        return -1

我堅持通過了 73 / 79 個測試用例。 特別是這個輸入

Input: "mississippi" (substring: "issip")
My output: -1
Expected Output: 4

我有點卡住了為什么我的代碼不起作用。

這是我運行您的代碼時得到的結果:

key,value,goodPos,goodCounter
0  ,m    ,None   ,0
1  ,i    ,None   ,0
2  ,s    ,1      ,1
3  ,s    ,1      ,2
4  ,i    ,1      ,3
5  ,s    ,1      ,4
6  ,s    ,None   ,0
7  ,i    ,None   ,0
8  ,p    ,7      ,1
9  ,p    ,None   ,0
10 ,i    ,None   ,0

他們的密鑰不斷增加,當它意識到 keyPos = 1 不是完全匹配時,密鑰已經設置為 position 7 ,到那時找到匹配項為時已晚。 我認為你需要一個嵌套的 for 循環。

編輯:

我決定自己嘗試一下,這是我想出的 for 循環:

    for key, value in enumerate(haystack):
        if needle[0] == value:
            goodPos = key
            for key, value in enumerate(needle):
                if (len(haystack) - goodPos) < len(needle):
                    print('Needle went past end of haystack. No match found')
                    return -1
                if value == haystack[goodPos + key]:
                    goodCounter += 1
                    if len(needle) == goodCounter:
                        print('Match found at goodPos = ', goodPos)
                        return goodPos
                else:
                    goodCounter = 0
        else:
            goodPos = ''
            goodCounter = 0
class Solution:
    def strStr(self, haystack: str, needle: str) -> int:
        result = -1
        try :
            result = haystack.index(needle)
        except:
            result = -1
        return result

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM