Content-Length: 209791 | pFad | http://github.com/youngyangyang04/leetcode-master/issues/2939
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
感觉算法竞赛版本的KMP算法更加精简,更好理解 采用字符串拼接方法,只需找到pi[i] == m的位置 附上解释链接:https://oi-wiki.org/string/kmp/
class Solution { public int strStr(String haystack, String needle) { int n = haystack.length(); int m = needle.length(); String s = needle + "#" + haystack; int[] pi = new int[s.length()]; for (int i = 1; i < s.length(); i++) { int len = pi[i - 1]; while (len > 0 && s.charAt(i) != s.charAt(len)) { len = pi[len - 1]; } if (s.charAt(i) == s.charAt(len)) { pi[i] = len + 1; if (pi[i] == m) { return i - m * 2; } } } return -1; } }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Fetched URL: http://github.com/youngyangyang04/leetcode-master/issues/2939
Alternative Proxies:
Alternative Proxy
pFad Proxy
pFad v3 Proxy
pFad v4 Proxy
感觉算法竞赛版本的KMP算法更加精简,更好理解
采用字符串拼接方法,只需找到pi[i] == m的位置
附上解释链接:https://oi-wiki.org/string/kmp/
The text was updated successfully, but these errors were encountered: