Implement strStr()
Description
For a given source string and a target string, you should output the first index(from 0) of target string in the source string.If the target does not exist in source, just return -1.
public class Solution {
/**
* @param source:
* @param target:
* @return: return the index
*/
public int strStr(String source, String target) {
// Write your code here
if(source == null ||target == null ){
return -1 ;
}
if(target.length() ==0){
return 0 ;
}
return source.indexOf(target) ;
}
}
该博客探讨了如何在Java中实现字符串搜索功能,通过`strStr()`方法查找目标字符串在源字符串中的索引。文章详细解释了代码逻辑,包括检查空指针、目标字符串长度为0的情况,并使用内置的`indexOf()`方法进行查找。此内容适用于理解字符串处理和算法应用。
102

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



