-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
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
【Day 83 】2024-02-06 - 28 实现 strStr( #87
Comments
|
|
class Solution:
def strStr(self, haystack: str, needle: str) -> int:
if needle not in haystack: return -1
for i in range(len(haystack)):
if haystack[i:len(needle)+i] == needle:
return i |
var strStr = function (haystack, needle) { |
思路暴力遍历(滑动窗口) 代码class Solution {
public int strStr(String haystack, String needle) {
int length_a = haystack.length();
int length_b = needle.length();
if (length_a<length_b){
return -1;
}
for (int i = 0; i <= length_a-length_b; i++) {
String substring = haystack.substring(i, i + length_b);
if (substring.equals(needle)){
return i;
}
}
return -1;
}
} 复杂度
|
class Solution {
public int strStr(String haystack, String needle) {
if (needle == null || needle.isEmpty()) {
return 0;
}
int i = 0;
while (i < haystack.length()) {
int j = 0;
int match = 0;
while (i + j < haystack.length() && j < needle.length() && haystack.charAt(i + j) == needle.charAt(j)) {
match++;
j++;
}
if (match == needle.length()) {
return i;
}
i++;
}
return -1;
}
} |
28 实现 strStr(
入选理由
暂无
题目地址
[ 之 BF&RK 篇)
https://leetcode-cn.com/problems/implement-strstr/]( 之 BF&RK 篇)
https://leetcode-cn.com/problems/implement-strstr/)
前置知识
题目描述
The text was updated successfully, but these errors were encountered: