TreeviewCopyright © aleen42 all right reserved, powered by aleen42

28. 实现 strStr()

https://leetcode-cn.com/problems/implement-strstr/

Java

/*
 * @Author: Goog Tech
 * @Date: 2020-08-25 16:42:06
 * @LastEditTime: 2020-08-25 16:42:32
 * @Description: https://leetcode-cn.com/problems/implement-strstr/
 * @FilePath: \leetcode-googtech\#28. Implement strStr()\Solution.java
 * @WebSite: https://algorithm.show/
 */

class Solution {

    // 😅理应使用 KMP 算法,这里简单使用 API 啦
    public int strStr(String haystack, String needle) {
        // 分别获取两个字符串的长度
        int needleLength = needle.length();
        int haystackLength = haystack.length();
        // 遍历字符串,其中 haystackLength - needleLength 是为了防止 substring 下标越界
        for(int i = 0; i <= haystackLength - needleLength; i++) {
            // 判断 needle 字符串是否与下标范围为 i 到 i + needleLength 所分割的字符串相等
            if(haystack.substring(i, i + needleLength).equals(needle)) {
                // 若相等则返回 needle 字符串出现的第一个位置的下标值
                return i;
            }
        }
        // 无果
        return -1;
    }
}

Python

'''
Author: Goog Tech
Date: 2020-08-25 16:42:12
LastEditTime: 2020-08-25 16:42:51
Description: https://leetcode-cn.com/problems/implement-strstr/
FilePath: \leetcode-googtech\#28. Implement strStr()\Solution.py
WebSite: https://algorithm.show/
'''

class Solution(object):

    # 😅理应使用 KMP 算法,这里简单使用 API 啦
    def strStr(self, haystack, needle):
        """
        :type haystack: str
        :type needle: str
        :rtype: int
        """
        # 判断 needle 字符串是否为空
        if needle == "": return 0
        # 分别获取两个字符串的长度
        haystackLength, needleLength = len(haystack), len(needle)
        # 遍历字符串,其中 len(haystack) - len(needle) 是为了防止 haystack[i : i + needleLength] 下标越界
        for i in range(0, haystackLength - needleLength + 1):
            # 判断 needle 字符串是否与下标范围为 i 到 i + needleLength 所分割的字符串相等
            if haystack[i : i + needleLength] == needle:
                # 若相等则返回 needle 字符串出现的第一个位置的下标值
                return i
        # 无果
        return -1
Copyright © GoogTech 2021 all right reserved,powered by GitbookLast update time : 2021-09-15 01:55:05

results matching ""

    No results matching ""