Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
这一次用java写的,确实比用c简单了许多。。。代码入下:
public class Solution {
public int strStr(String haystack, String needle) {
if(haystack.contains(needle)){
return haystack.indexOf(needle);
}else{
return -1;
}
}
}
本文介绍了一种使用Java语言实现strStr()函数的方法。该函数用于返回子字符串在主字符串中首次出现的位置索引,如果子字符串不存在于主字符串,则返回-1。通过使用Java内置的contains()和indexOf()方法简化了实现过程。

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



