{"id":6477,"date":"2022-06-05T16:59:32","date_gmt":"2022-06-05T08:59:32","guid":{"rendered":"https:\/\/blog.iyatt.com\/?p=6477"},"modified":"2024-05-06T07:58:49","modified_gmt":"2024-05-05T23:58:49","slug":"%e5%ae%9e%e7%8e%b0-strstr","status":"publish","type":"post","link":"https:\/\/blog.iyatt.com\/?p=6477","title":{"rendered":"\u5b9e\u73b0 strStr"},"content":{"rendered":"\n<p class=\"has-text-align-center\">\u9898\u76ee<\/p>\n\n\n<p>\u7ed9\u4f60\u4e24\u4e2a\u5b57\u7b26\u4e32\u00a0haystack \u548c needle \uff0c\u8bf7\u4f60\u5728 haystack \u5b57\u7b26\u4e32\u4e2d\u627e\u51fa needle \u5b57\u7b26\u4e32\u51fa\u73b0\u7684\u7b2c\u4e00\u4e2a\u4f4d\u7f6e\uff08\u4e0b\u6807\u4ece 0 \u5f00\u59cb\uff09\u3002\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u00a0 -1 \u3002<\/p>\n\n\n<p>\u8bf4\u660e\uff1a<\/p>\n\n\n<p>\u5f53\u00a0needle\u00a0\u662f\u7a7a\u5b57\u7b26\u4e32\u65f6\uff0c\u6211\u4eec\u5e94\u5f53\u8fd4\u56de\u4ec0\u4e48\u503c\u5462\uff1f\u8fd9\u662f\u4e00\u4e2a\u5728\u9762\u8bd5\u4e2d\u5f88\u597d\u7684\u95ee\u9898\u3002<\/p>\n\n\n<p>\u5bf9\u4e8e\u672c\u9898\u800c\u8a00\uff0c\u5f53\u00a0needle\u00a0\u662f\u7a7a\u5b57\u7b26\u4e32\u65f6\u6211\u4eec\u5e94\u5f53\u8fd4\u56de 0 \u3002\u8fd9\u4e0e C \u8bed\u8a00\u7684\u00a0strstr()\u00a0\u4ee5\u53ca Java \u7684\u00a0indexOf()\u00a0\u5b9a\u4e49\u76f8\u7b26\u3002<\/p>\n\n\n<p>\u6765\u6e90\uff1a\u529b\u6263\uff08LeetCode\uff09<br>\u94fe\u63a5\uff1ahttps:\/\/leetcode.cn\/problems\/implement-strstr<br>\u8457\u4f5c\u6743\u5f52\u9886\u6263\u7f51\u7edc\u6240\u6709\u3002\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u8054\u7cfb\u5b98\u65b9\u6388\u6743\uff0c\u975e\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u51fa\u5904\u3002<\/p>\n\n\n<p class=\"has-text-align-center\">\u5206\u6790\u53ca\u5b9e\u73b0<\/p>\n\n\n<p>\u8fd9\u91cc\u662f\u4ece\u7b97\u6cd5\u7684\u89d2\u5ea6\u8003\u8651\u95ee\u9898\uff0c\u6240\u4ee5\u76f4\u63a5\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u7684\u65b9\u5f0f\u5c31\u4e0d\u8003\u8651\u4e86\uff0c\u505a\u51fa\u6765\u4e5f\u6ca1\u6709\u610f\u4e49\u3002<\/p>\n\n\n<ul class=\"wp-block-list\"><li>\u66b4\u529b<\/li><\/ul>\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"550\" height=\"808\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/06\/image-15.png\" alt=\"\" class=\"wp-image-6482 lazyload\" data-srcset=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/06\/image-15.png 550w, https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/06\/image-15-204x300.png 204w\" data-sizes=\"(max-width: 550px) 100vw, 550px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 550px; --smush-placeholder-aspect-ratio: 550\/808;\" \/><\/figure>\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">int strStr(char * haystack, char * needle)\n{\n    if (strlen(needle) == 0)\n    {\n        return 0;\n    }\n\n    int haystack_len = strlen(haystack);\n    int needle_len = strlen(needle);\n\n    for (int i = 0; i &lt;= haystack_len - needle_len; ++i)\n    {\n        static int j;\n        for (j = 0; j &lt; needle_len; ++j)\n        {\n            if (haystack[i + j] != needle[j])\n            {\n                break;\n            }\n        }\n        if (j == needle_len)\n        {\n            return i;\n        }\n    }\n    return -1;\n}<\/pre>\n\n\n<p>0 ms \u662f\u4e0d\u53ef\u80fd\u7b49\u4e8e 0 \u7684\uff0c\u53ef\u80fd\u6d4b\u8bd5\u4f8b\u5b50\u6570\u91cf\u5c11\uff0c\u5df2\u7ecf\u88ab\u56db\u820d\u4e94\u5165\u4e3a 0 ms \u4e86\u3002<\/p>\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"408\" height=\"134\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/06\/image-13.png\" alt=\"\" class=\"wp-image-6478 lazyload\" data-srcset=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/06\/image-13.png 408w, https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/06\/image-13-300x99.png 300w\" data-sizes=\"(max-width: 408px) 100vw, 408px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 408px; --smush-placeholder-aspect-ratio: 408\/134;\" \/><\/figure>\n\n\n<ul class=\"wp-block-list\"><li>KMP \u7b97\u6cd5<\/li><\/ul>\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">void kmp_init(const char *s, int *prefix, size_t size) {\n    prefix[0] = 0;\n    for (size_t i = 1; i &lt; size; ++i) {\n        if (s[i] == s[prefix[i - 1]])\n            prefix[i] = prefix[i - 1] + 1;\n        else {\n            int j = i - 1;\n            while (prefix[j] &gt; 0 &amp;&amp; s[prefix[j]] != s[i])\n                j = prefix[j] - 1;\n            if (prefix[j] &gt; 0)\n                prefix[i] = prefix[j] + 1;\n            else {\n                prefix[i] = (s[i] == s[0]);\n            }\n        }\n    }\n}\n\nint strStr(const char *src, const char *dest) {\n    if (!dest || !src)\n        return -1;\n    if (!*dest)\n        return 0;\n    size_t size = strlen(dest);\n    int *prefix = malloc(sizeof(int) * size);\n    kmp_init(dest, prefix, size);\n    size_t i, j;\n    for (i = j = 0; src[i] &amp;&amp; j &lt; size; ++i) {\n        if (dest[j] == src[i]) {\n            ++j;\n        }\n        else if (j) {\n            while (prefix[j - 1] &gt; 0 &amp;&amp; dest[prefix[j - 1]] != src[i])\n                j = prefix[j - 1];\n            if (prefix[j - 1] &gt; 0) {\n                j = prefix[j - 1] + 1;\n            }\n            else {\n                j = (dest[0] == src[i]);\n            }\n        }\n    }\n    free(prefix);\n    if (j &lt; size)\n        return -1;\n    return i - size;\n}<\/pre>\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"386\" height=\"140\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/06\/image-16.png\" alt=\"\" class=\"wp-image-6485 lazyload\" data-srcset=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/06\/image-16.png 386w, https:\/\/blog.iyatt.com\/wp-content\/uploads\/2022\/06\/image-16-300x109.png 300w\" data-sizes=\"(max-width: 386px) 100vw, 386px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 386px; --smush-placeholder-aspect-ratio: 386\/140;\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee \u7ed9\u4f60\u4e24\u4e2a\u5b57\u7b26\u4e32\u00a0haystack \u548c needle \uff0c\u8bf7\u4f60\u5728 haystack \u5b57\u7b26\u4e32\u4e2d\u627e\u51fa needl [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"zakra_page_container_layout":"customizer","zakra_page_sidebar_layout":"customizer","zakra_remove_content_margin":false,"zakra_sidebar":"customizer","zakra_transparent_header":"customizer","zakra_logo":0,"zakra_main_header_style":"default","zakra_menu_item_color":"","zakra_menu_item_hover_color":"","zakra_menu_item_active_color":"","zakra_menu_active_style":"","zakra_page_header":true,"_lmt_disableupdate":"no","_lmt_disable":"no","footnotes":""},"categories":[1,613],"tags":[],"class_list":["post-6477","post","type-post","status-publish","format-standard","hentry","category-all","category-613"],"modified_by":"IYATT-yx","_links":{"self":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/6477","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6477"}],"version-history":[{"count":0,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/6477\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6477"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6477"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6477"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}