zoukankan      html  css  js  c++  java
  • leetcode28:实现strStr()

    实现 strStr() 函数。

    给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回  -1。

    示例 1:

    输入: haystack = "hello", needle = "ll"
    输出: 2
    示例 2:

    输入: haystack = "aaaaa", needle = "bba"
    输出: -1
    说明:

    当 needle 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。

    对于本题而言,当 needle 是空字符串时我们应当返回 0 。这与C语言的 strstr() 以及 Java的 indexOf() 定义相符。

    Sunday匹配==》核心思想是:在匹配过程中,模式串发现不匹配时,算法能跳过尽可能多的字符以进行下一步的匹配,从而提高了匹配效率。

    • 对齐目标串和模式串,从前向后匹配
    • 关注主串中位于模式串后面的第一个元素(核心)
    • 如果关注的字符没有在子串中出现则直接跳过
    • 否则开始移动模式串,移动位数 = 子串长度 - 该字符最右出现的位置(以0开始)==>移到下一个元素和模式串中最后一个该字符对齐

    =============================================Python============================================

    class Solution:
        def strStr(self, haystack: str, needle: str) -> int:
            def findPos(a, arr):
                for i in range(len(arr)):
                    if arr[len(arr)-i-1] == a:
                        return len(arr) - i - 1
                return -1
            
            if not needle:
                return 0
            if not haystack:
                return -1
            if len(haystack) < len(needle):
                return -1
            
            l = len(needle)
            i = 0
            j = 0
            while j < len(needle):
                if i > len(haystack) - 1:
                    return -1
                if needle[j] == haystack[i]:
                    i += 1
                    j += 1
                else:
                    nextInd = len(needle) - j + i
                    if nextInd < len(haystack):
                        tmp = findPos(haystack[nextInd], needle)
                        if tmp == -1:
                            i = nextInd + 1
                        else:
                            i = nextInd - tmp
    // i += len(needle) - j - tmp j
    = 0 else: return - 1 return i - j

    ============================================Java=================================

    class Solution {
        public int strStr(String haystack, String needle) {
            if (haystack == null || needle == null) {
                return 0;
            }
            if (haystack.length() < needle.length()) {
                return -1;
            }
            //目标串匹配索引
            int originIndex = 0;
            //模式串匹配索引
            int aimIndex = 0;
            //成功匹配完终止条件:所有aim均成功匹配
            while (aimIndex < needle.length()) {
                //针对origin匹配完,但aim未匹配完情况处理
                if (originIndex > haystack.length() - 1) {
                    return -1;
                }
                if (haystack.charAt(originIndex) == needle.charAt(aimIndex)) {
                    //匹配index均加1
                    originIndex++;
                    aimIndex++;
                } else {
                    int nextCharIndex = originIndex - aimIndex + needle.length();
                    //判断下一个目标字符是否存在
                    if (nextCharIndex < haystack.length()){
                        //判断目标字符在模式串中匹配到,返回最后一个匹配的index
                        int step = needle.lastIndexOf(haystack.charAt(nextCharIndex));
                        if (step == -1) {
                            //不存在的话,设置到目标字符的下一个元素
                            originIndex = nextCharIndex + 1;
                        } else {
                            originIndex = nextCharIndex - step;
                        }
                        //模式串总是从第一个开始匹配
                        aimIndex = 0;
                    } else {
                        return -1;
                    }
                }
            }
            return originIndex - aimIndex;
        }
    }

    双指针

    class Solution:
        def strStr(self, haystack: str, needle: str) -> int:
            if needle is '':
                return 0
            if len(needle) > len(haystack):
                return -1
            i = 0
            while i < len(haystack) - len(needle) + 1:
                j = 0
                while j < len(needle) and needle[j] == haystack[i + j]:
                    if j == len(needle) - 1:
                        return i
                    j += 1
                i += 1
            return -1

    RollingHash

  • 相关阅读:
    java 编码问题
    关于时间
    页面
    关于微信
    01-jQuery的介绍
    15-BOM
    14-定时器
    13-JS中的面向对象
    12-关于DOM操作的相关案例
    购物车练习
  • 原文地址:https://www.cnblogs.com/liushoudong/p/13512840.html
Copyright © 2011-2022 走看看