{"id":4129,"date":"2023-11-05T12:52:35","date_gmt":"2023-11-05T04:52:35","guid":{"rendered":"https:\/\/white-night.club\/?p=4129"},"modified":"2023-11-09T11:07:43","modified_gmt":"2023-11-09T03:07:43","slug":"ad130","status":"publish","type":"post","link":"https:\/\/white-night.club\/index.php\/2023\/11\/05\/ad130\/","title":{"rendered":"28.\u00a0\u627e\u51fa\u5b57\u7b26\u4e32\u4e2d\u7b2c\u4e00\u4e2a\u5339\u914d\u9879\u7684\u4e0b\u6807"},"content":{"rendered":"\n<p>\u7ed9\u4f60\u4e24\u4e2a\u5b57\u7b26\u4e32\u00a0<code>haystack<\/code>\u00a0\u548c\u00a0<code>needle<\/code>\u00a0\uff0c\u8bf7\u4f60\u5728\u00a0<code>haystack<\/code>\u00a0\u5b57\u7b26\u4e32\u4e2d\u627e\u51fa\u00a0<code>needle<\/code>\u00a0\u5b57\u7b26\u4e32\u7684\u7b2c\u4e00\u4e2a\u5339\u914d\u9879\u7684\u4e0b\u6807\uff08\u4e0b\u6807\u4ece 0 \u5f00\u59cb\uff09\u3002\u5982\u679c\u00a0<code>needle<\/code>\u00a0\u4e0d\u662f\u00a0<code>haystack<\/code>\u00a0\u7684\u4e00\u90e8\u5206\uff0c\u5219\u8fd4\u56de\u00a0\u00a0<code>-1<\/code><strong>\u00a0<\/strong>\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>haystack = \"sadbutsad\", needle = \"sad\"\n<strong>\u8f93\u51fa\uff1a<\/strong>0\n<strong>\u89e3\u91ca\uff1a<\/strong>\"sad\" \u5728\u4e0b\u6807 0 \u548c 6 \u5904\u5339\u914d\u3002\n\u7b2c\u4e00\u4e2a\u5339\u914d\u9879\u7684\u4e0b\u6807\u662f 0 \uff0c\u6240\u4ee5\u8fd4\u56de 0 \u3002\n<\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>haystack = \"leetcode\", needle = \"leeto\"\n<strong>\u8f93\u51fa\uff1a<\/strong>-1\n<strong>\u89e3\u91ca\uff1a<\/strong>\"leeto\" \u6ca1\u6709\u5728 \"leetcode\" \u4e2d\u51fa\u73b0\uff0c\u6240\u4ee5\u8fd4\u56de -1 \u3002\n<\/pre>\n\n\n\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n\n\n\n<ul>\n<li><code>1 &lt;= haystack.length, needle.length &lt;= 10<sup>4<\/sup><\/code><\/li>\n\n\n\n<li><code>haystack<\/code>&nbsp;\u548c&nbsp;<code>needle<\/code>&nbsp;\u4ec5\u7531\u5c0f\u5199\u82f1\u6587\u5b57\u7b26\u7ec4\u6210<\/li>\n<\/ul>\n\n\n\n<div style=\"height:60px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<blockquote class=\"wp-block-quote has-luminous-vivid-amber-color has-text-color\">\n<p>\u89e3\u9898\u601d\u8def<\/p>\n<cite><em>\u53cc\u6307\u9488<\/em><\/cite><\/blockquote>\n\n\n\n<p>\u5e38\u89c4\u7684\u53cc\u6307\u9488\u9898\uff0c\u5176\u5b9e\u5c31\u662f\u66b4\u529b\u3002\u8fd8\u53ef\u4ee5\u7528kmp\u505a\uff0c\u4f46\u662f\u597d\u4e45\u6ca1\u5199kmp\u4e0d\u592a\u8bb0\u5f97\u600e\u4e48\u5199\u4e86\u3002\u5230\u65f6\u5019\u7cfb\u7edf\u590d\u4e60\u7b97\u6cd5\u7684\u65f6\u5019\u518d\u91cd\u65b0\u5199\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution {\r\npublic:\r\n    int strStr(string haystack, string needle) {\r\n        int hayLen=haystack.length();\r\n        int needLen=needle.length();\r\n        int a=0,b=0;\r\n        for(int i=0;i&lt;hayLen;)\r\n        {\r\n            if(b==needLen)\r\n            {\r\n                return i;\r\n            }\r\n            if(haystack&#91;a]==needle&#91;b])\r\n            {\r\n                a++;\r\n                b++;\r\n            }else\r\n            {\r\n                i++;\r\n                a=i;\r\n                b=0;\r\n            }\r\n        }\r\n        return -1;\r\n    }\r\n};<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Leetcode\u9898\u76ee\uff1a\u53cc\u6307\u9488<\/p>\n","protected":false},"author":1,"featured_media":2827,"comment_status":"open","ping_status":"","sticky":false,"template":"base","format":"standard","meta":{"footnotes":""},"categories":[42,25],"tags":[38],"_links":{"self":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/4129"}],"collection":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/comments?post=4129"}],"version-history":[{"count":0,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/4129\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media\/2827"}],"wp:attachment":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media?parent=4129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/categories?post=4129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/tags?post=4129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}