題目在此 28. Implement strStr()
實作 C 語言的 strstr()
解題思維
不解釋,直接開幹
Time complexity: O(n)
程式碼
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| class Solution: def strStr(self, haystack: str, needle: str) -> int: if not haystack or not needle: return 0 main_length = len(haystack) check_length = len(needle) if main_length == 1: return 0 for i in range(main_length): if haystack[i:i + check_length] == needle: return i return -1
|
也許你也會想看看