Skip to content

LeetCode: 28. 实现strStr() #71

Open

Description

@resse92

遍历遍历遍历

class Solution:
    def strStr(self, haystack, needle):
        """
        :type haystack: str
        :type needle: str
        :rtype: int
        """
        if len(needle) == 0:
            return 0
        
        needle_len = len(needle)
        haystack_len = len(haystack)
        if needle_len > haystack_len:
            return -1
        
        for index in range(haystack_len - needle_len + 1):
            find = 0
            for s2 in needle:
                if haystack_len > find + index and s2 == haystack[find + index]:
                    find += 1
                else:
                    break
            if find == needle_len:
                return index
        return -1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions