题意:在一个字符串中查找是否存在子串和其他的字符串相同
思路:暴力破解
代码:
package ImplementStr;
public class ImplementStr {
public String FindStr(String haystack , String needle){
int m = haystack.length();
int n = needle.length();
if(m < n || m == 0) return null;
for(int i =0 ; i < m ; i++){
int k = i;
int j = 0;
while(j < needle.length() && k < haystack.length() && haystack.charAt(k)== needle.charAt(j)){
k++ ;
j++ ;
if(j == n) return needle;
}
}
return null;
}
public static void main(String[] args) {
ImplementStr isStr = new ImplementStr();
String haystack = "i love java";
String needle = "java";
String string = isStr.FindStr(haystack, needle);
System.out.println(string);
}
}
本文介绍了一个简单的子串查找算法实现,通过遍历目标字符串来判断是否包含指定的子串。该算法适用于基本的字符串匹配场景。
652

被折叠的 条评论
为什么被折叠?



