{"id":300031,"date":"2024-05-20T17:32:16","date_gmt":"2024-05-20T09:32:16","guid":{"rendered":""},"modified":"2024-05-20T17:32:30","modified_gmt":"2024-05-20T09:32:30","slug":"java-%e7%bc%96%e7%a8%8b%e4%b8%8b%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0-strstr-%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/300031.html","title":{"rendered":"java \u7f16\u7a0b\u4e0b\u5982\u4f55\u5b9e\u73b0 strStr \u51fd\u6570"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26212150\/0b20d632-0581-42fd-acaa-d94b01f8634a-1.webp\" alt=\"java \u7f16\u7a0b\u4e0b\u5982\u4f55\u5b9e\u73b0 strStr \u51fd\u6570\" \/><\/p>\n<p><p>\u5b9e\u73b0strStr\u51fd\u6570\u7684\u76ee\u7684\u662f\u5728\u4e00\u4e2a\u4e3b\u5b57\u7b26\u4e32\uff08haystack\uff09\u4e2d\u67e5\u627e\u4e00\u4e2a\u5b50\u5b57\u7b26\u4e32\uff08needle\uff09\u7684\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u4f4d\u7f6e\uff0c\u5982\u679c\u4e0d\u5b58\u5728\u5219\u8fd4\u56de-1\u3002<strong>Java\u7f16\u7a0b\u5b9e\u73b0strStr\u51fd\u6570\u7684\u5e38\u89c1\u65b9\u6cd5\u6709\u66b4\u529b\u5339\u914d\u6cd5\u3001KMP\u7b97\u6cd5<\/strong>\u3001Rabin-Karp\u7b97\u6cd5\u7b49\u3002\u5176\u4e2d\uff0c<strong>\u66b4\u529b\u5339\u914d\u6cd5<\/strong>\u662f\u6700\u76f4\u89c2\u7684\u65b9\u6cd5\uff0c\u5b83\u901a\u8fc7\u904d\u5386\u4e3b\u5b57\u7b26\u4e32\u6765\u9010\u4e2a\u6bd4\u8f83\u5b50\u4e32\uff0c\u901a\u5e38\u662f\u5b9e\u73b0\u6700\u7b80\u5355\u7684\u505a\u6cd5\uff0c\u4f46\u5728\u6700\u574f\u60c5\u51b5\u4e0b\u6027\u80fd\u4f1a\u663e\u8457\u964d\u4f4e\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001<strong>JAVA \u4e2d\u7684\u66b4\u529b\u5339\u914d\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>\u66b4\u529b\u5339\u914d\u6cd5\u901a\u8fc7\u5916\u5c42\u5faa\u73af\u904d\u5386\u4e3b\u5b57\u7b26\u4e32\uff0c\u5185\u5c42\u5faa\u73af\u904d\u5386\u5b50\u5b57\u7b26\u4e32\uff0c\u5e76\u8fdb\u884c\u9010\u4e2a\u5b57\u7b26\u7684\u6bd4\u8f83\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">public int strStr(String haystack, String needle) {<\/p>\n<p>    int L = needle.length(), n = haystack.length();<\/p>\n<p>    if (L == 0) {<\/p>\n<p>        return 0;<\/p>\n<p>    }<\/p>\n<p>    for (int start = 0; start &lt; n - L + 1; ++start) {<\/p>\n<p>        if (haystack.substring(start, start + L).equals(needle)) {<\/p>\n<p>            return start;<\/p>\n<p>        }<\/p>\n<p>    }<\/p>\n<p>    return -1;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u68c0\u67e5\u5b50\u5b57\u7b26\u4e32\u957f\u5ea6\u4e3a\u96f6\u7684\u7279\u6b8a\u60c5\u51b5\u3002\u5982\u679c\u957f\u5ea6\u4e3a\u96f6\uff0c\u8868\u793a\u627e\u5230\u4e86\u5339\u914d\u7684\u4f4d\u7f6e\uff0c\u4e14\u4e3a0\u3002\u7136\u540e\uff0c\u6211\u4eec\u904d\u5386\u4e3b\u5b57\u7b26\u4e32\uff0c\u76f4\u5230\u5269\u4f59\u7684\u5b57\u7b26\u4e0d\u8db3\u4ee5\u4e0e\u5b50\u5b57\u7b26\u4e32\u5339\u914d\u4e3a\u6b62\u3002\u904d\u5386\u65f6\uff0c\u6211\u4eec\u6bd4\u8f83\u5f53\u524d\u7684\u5b50\u6bb5\u662f\u5426\u7b49\u4e8e\u5b50\u5b57\u7b26\u4e32\uff0c\u5982\u679c\u76f8\u7b49\uff0c\u5219\u8fd4\u56de\u5f53\u524d\u7684\u8d77\u70b9index\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001<strong>JAVA \u4e2d\u7684 KMP \u7b97\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>Knuth-Morris-Pratt\u7b97\u6cd5\uff08KMP\uff09\u662f\u4e00\u79cd\u6539\u8fdb\u7684\u5b57\u7b26\u4e32\u5339\u914d\u7b97\u6cd5\uff0c\u5b83\u5728\u5339\u914d\u5931\u8d25\u65f6\uff0c\u91cd\u65b0\u5f00\u59cb\u5339\u914d\u65f6\u80fd\u591f\u8df3\u8fc7\u4e00\u4e9b\u4e0d\u5fc5\u8981\u7684\u6bd4\u8f83\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">public int strStr(String haystack, String needle) {<\/p>\n<p>    if (needle.isEmpty()) return 0;<\/p>\n<p>    int[] lps = computeTemporaryArray(needle);<\/p>\n<p>    int i = 0, j = 0;<\/p>\n<p>    int M = needle.length(), N = haystack.length();<\/p>\n<p>    while (i &lt; N) {<\/p>\n<p>        if (haystack.charAt(i) == needle.charAt(j)) {<\/p>\n<p>            i++;<\/p>\n<p>            j++;<\/p>\n<p>        }<\/p>\n<p>        if (j == M) {<\/p>\n<p>            return i - j;<\/p>\n<p>        } else if (i &lt; N &amp;&amp; haystack.charAt(i) != needle.charAt(j)) {<\/p>\n<p>            if (j != 0) {<\/p>\n<p>                j = lps[j - 1];<\/p>\n<p>            } else {<\/p>\n<p>                i = i + 1;<\/p>\n<p>            }<\/p>\n<p>        }<\/p>\n<p>    }<\/p>\n<p>    return -1;<\/p>\n<p>}<\/p>\n<p>private int[] computeTemporaryArray(String needle) {<\/p>\n<p>    int[] lps = new int[needle.length()];<\/p>\n<p>    int index = 0;<\/p>\n<p>    for(int i = 1; i &lt; needle.length();) {<\/p>\n<p>        if (needle.charAt(i) == needle.charAt(index)) {<\/p>\n<p>            lps[i] = index + 1;<\/p>\n<p>            index++;<\/p>\n<p>            i++;<\/p>\n<p>        } else {<\/p>\n<p>            if (index != 0) {<\/p>\n<p>                index = lps[index - 1];<\/p>\n<p>            } else {<\/p>\n<p>                lps[i] = 0;<\/p>\n<p>                i++;<\/p>\n<p>            }<\/p>\n<p>        }<\/p>\n<p>    }<\/p>\n<p>    return lps;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u751f\u6210\u4e86\u6240\u8c13\u7684\u201c\u6700\u957f\u516c\u5171\u524d\u540e\u7f00\u201d\u6570\u7ec4\uff0c\u8fd9\u4e2a\u6570\u7ec4\u7528\u6765\u7f29\u77ed\u5728\u4e0d\u5339\u914d\u65f6\u56de\u6eaf\u7684\u8ddd\u79bb\u3002\u4e00\u65e6\u53d1\u73b0\u5269\u4e0b\u7684\u90e8\u5206\u4e0d\u5339\u914d\uff0c\u6211\u4eec\u5c31\u4f7f\u7528\u8fd9\u4e2a\u6570\u7ec4\u6765\u786e\u5b9a\u4e0b\u4e00\u4e2a\u4f4d\u7f6e\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u5b57\u7b26\u4e00\u4e2a\u5b57\u7b26\u5730\u79fb\u52a8\u3002\u8fd9\u5927\u5927\u63d0\u9ad8\u4e86\u6548\u7387\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001<strong>JAVA \u4e2d\u7684 Rabin-Karp \u7b97\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>Rabin-Karp\u7b97\u6cd5\u662f\u901a\u8fc7\u8ba1\u7b97\u5b57\u7b26\u4e32\u7684\u6563\u5217\u503c\u6765\u52a0\u901f\u5b57\u7b26\u4e32\u5339\u914d\u8fc7\u7a0b\u7684\u4e00\u79cd\u7b97\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">public int strStr(String haystack, String needle) {<\/p>\n<p>    int L = needle.length(), n = haystack.length();<\/p>\n<p>    if (L &gt; n) return -1;<\/p>\n<p>    \/\/ base value for the rolling hash function<\/p>\n<p>    int a = 26;<\/p>\n<p>    \/\/ modulus value for the rolling hash function to avoid overflow<\/p>\n<p>    long modulus = (long)Math.pow(2, 31);<\/p>\n<p>    \/\/ compute the hash of strings haystack[:L], needle[:L]<\/p>\n<p>    long h = 0, ref_h = 0;<\/p>\n<p>    for (int i = 0; i &lt; L; ++i) {<\/p>\n<p>        h = (h * a + charToInt(haystack.charAt(i), a)) % modulus;<\/p>\n<p>        ref_h = (ref_h * a + charToInt(needle.charAt(i), a)) % modulus;<\/p>\n<p>    }<\/p>\n<p>    if (h == ref_h) return 0;<\/p>\n<p>    \/\/ const value to be used often : aL % modulus<\/p>\n<p>    long aL = 1;<\/p>\n<p>    for (int i = 1; i &lt;= L; ++i) aL = (aL * a) % modulus;<\/p>\n<p>    for (int start = 1; start &lt; n - L + 1; ++start) {<\/p>\n<p>        \/\/ compute rolling hash in O(1) time<\/p>\n<p>        h = (h * a - charToInt(haystack.charAt(start - 1), a) * aL<\/p>\n<p>                + charToInt(haystack.charAt(start + L - 1), a)) % modulus;<\/p>\n<p>        if (h == ref_h) return start;<\/p>\n<p>    }<\/p>\n<p>    return -1;<\/p>\n<p>}<\/p>\n<p>private int charToInt(int idx, int a) {<\/p>\n<p>    return (idx - &#039;a&#039;);<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>Rabin-Karp\u7b97\u6cd5\u5229\u7528\u4e86\u6563\u5217\u51fd\u6570\u6765\u907f\u514d\u5728\u6bcf\u4e2a\u6b65\u9aa4\u4e2d\u68c0\u67e5\u6240\u6709\u5b57\u7b26\u3002\u53ef\u4ee5\u5b9e\u73b0\u5728\u5e73\u5747\u548c\u6700\u597d\u60c5\u51b5\u4e0b\u90fd\u662f\u7ebf\u6027\u65f6\u95f4\u590d\u6742\u5ea6\u7684\u7b97\u6cd5\uff0c\u4f46\u6700\u574f\u60c5\u51b5\u4e0b\u4f1a\u964d\u81f3\u5e73\u65b9\u7ea7\u522b\u3002\u6b64\u7b97\u6cd5\u7684\u6838\u5fc3\u662f\u6709\u6548\u7684\u8ba1\u7b97\u6eda\u52a8\u6563\u5217\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5728\u5e38\u6570\u65f6\u95f4\u5185\u4ece\u6563\u5217\u4e2d\u52a0\u5165\u548c\u5220\u9664\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u73b0\u9ad8\u6548\u7684strStr\u51fd\u6570\u65f6\uff0c\u9700\u8981\u6839\u636e\u4e0d\u540c\u60c5\u5883\u9009\u62e9\u6700\u4f73\u7684\u5b57\u7b26\u4e32\u5339\u914d\u7b97\u6cd5\u3002\u867d\u7136\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u7b80\u5355\u7684\u66b4\u529b\u5339\u914d\u65b9\u6cd5\u8db3\u591f\u597d\uff0c\u4f46\u638c\u63e1KMP\u6216Rabin-Karp\u7b49\u7b97\u6cd5\u80fd\u591f\u89e3\u51b3\u4e00\u4e9b\u7279\u6b8a\u7684\u3001\u9700\u8981\u66f4\u9ad8\u6548\u5339\u914d\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>Q1: Java\u7f16\u7a0b\u4e2d\u5982\u4f55\u5b9e\u73b0strStr\u51fd\u6570\uff1f<\/strong><\/p>\n<p>A1: \u5728Java\u4e2d\u5b9e\u73b0strStr\u51fd\u6570\u53ef\u4ee5\u4f7f\u7528\u66b4\u529b\u5339\u914d\u6216\u8005KMP\u7b97\u6cd5\u3002\u66b4\u529b\u5339\u914d\u662f\u6700\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7\u904d\u5386\u539f\u5b57\u7b26\u4e32\u548c\u76ee\u6807\u5b57\u7b26\u4e32\uff0c\u4f9d\u6b21\u5339\u914d\u6bcf\u4e00\u4e2a\u5b57\u7b26\uff0c\u5982\u679c\u5339\u914d\u5931\u8d25\u5219\u6bd4\u8f83\u4e0b\u4e00\u4e2a\u5b57\u7b26\u3002KMP\u7b97\u6cd5\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u5b57\u7b26\u4e32\u5339\u914d\u7b97\u6cd5\uff0c\u5b83\u901a\u8fc7\u9884\u5904\u7406\u6a21\u5f0f\u5b57\u7b26\u4e32\uff0c\u5229\u7528\u5339\u914d\u5931\u8d25\u65f6\u7684\u4fe1\u606f\u8df3\u8fc7\u4e00\u4e9b\u4e0d\u5fc5\u8981\u7684\u6bd4\u8f83\uff0c\u4ece\u800c\u63d0\u9ad8\u5339\u914d\u7684\u6548\u7387\u3002<\/p>\n<p><strong>Q2: Java\u7f16\u7a0b\u4e2d\u5982\u4f55\u5904\u7406strStr\u51fd\u6570\u5339\u914d\u5931\u8d25\u7684\u60c5\u51b5\uff1f<\/strong><\/p>\n<p>A2: \u5728Java\u7f16\u7a0b\u4e2d\uff0c\u5f53strStr\u51fd\u6570\u5728\u539f\u5b57\u7b26\u4e32\u4e2d\u627e\u4e0d\u5230\u76ee\u6807\u5b57\u7b26\u4e32\u65f6\uff0c\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a\u7279\u5b9a\u7684\u503c\u6765\u8868\u793a\u5339\u914d\u5931\u8d25\u3002\u5e38\u89c1\u7684\u505a\u6cd5\u662f\u8fd4\u56de-1\uff0c\u8868\u793a\u672a\u627e\u5230\u76ee\u6807\u5b57\u7b26\u4e32\u3002\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u4e00\u4e2a\u53d8\u91cf\u6765\u8bb0\u5f55\u5339\u914d\u7684\u4f4d\u7f6e\uff0c\u5f53\u5339\u914d\u5931\u8d25\u65f6\u8fd4\u56de-1\uff0c\u5426\u5219\u8fd4\u56de\u5339\u914d\u7684\u8d77\u59cb\u4f4d\u7f6e\u3002<\/p>\n<p><strong>Q3: Java\u7f16\u7a0b\u4e2d\u5982\u4f55\u5904\u7406strStr\u51fd\u6570\u7684\u8fb9\u754c\u60c5\u51b5\uff1f<\/strong><\/p>\n<p>A3: \u5728Java\u7f16\u7a0b\u4e2d\uff0c\u5904\u7406strStr\u51fd\u6570\u7684\u8fb9\u754c\u60c5\u51b5\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u9700\u8981\u8003\u8651\u4ee5\u4e0b\u51e0\u79cd\u60c5\u51b5\uff1a1\uff09\u5f53\u539f\u5b57\u7b26\u4e32\u6216\u76ee\u6807\u5b57\u7b26\u4e32\u4e3a\u7a7a\u65f6\uff0c\u5e94\u8be5\u8fd4\u56de\u5339\u914d\u5931\u8d25\uff1b2\uff09\u5f53\u76ee\u6807\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u5927\u4e8e\u539f\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u65f6\uff0c\u65e0\u9700\u505a\u5339\u914d\u64cd\u4f5c\uff0c\u76f4\u63a5\u8fd4\u56de\u5339\u914d\u5931\u8d25\uff1b3\uff09\u5982\u679c\u5339\u914d\u6210\u529f\uff0c\u9700\u8981\u6ce8\u610f\u8fb9\u754c\u60c5\u51b5\uff0c\u5373\u76ee\u6807\u5b57\u7b26\u4e32\u7684\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u5339\u914d\u5230\u539f\u5b57\u7b26\u4e32\u7684\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u8fd4\u56de\u5339\u914d\u7684\u8d77\u59cb\u4f4d\u7f6e\uff0c\u6216\u8005\u6839\u636e\u9700\u6c42\u8fd4\u56de\u4e00\u4e2a\u7279\u5b9a\u7684\u503c\u8868\u793a\u5339\u914d\u6210\u529f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5b9e\u73b0strStr\u51fd\u6570\u7684\u76ee\u7684\u662f\u5728\u4e00\u4e2a\u4e3b\u5b57\u7b26\u4e32\uff08haystack\uff09\u4e2d\u67e5\u627e\u4e00\u4e2a\u5b50\u5b57\u7b26\u4e32\uff08needle\uff09\u7684\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684 [&hellip;]","protected":false},"author":3,"featured_media":300051,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/300031"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=300031"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/300031\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/300051"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=300031"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=300031"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=300031"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}