描述
对于一个给定的 source 字符串和一个 target 字符串,你应该在 source 字符串中找出 target 字符串出现的第一个位置(从0开始)。如果不存在,则返回 -1。
思路
- 嵌套循环读取source和target字符串,用变量k记录起始位置
- 当两者初次出现字符相等时,起始位置加一,判断下一个字符是否相等
- 直到完全读取完target字符
public class Solution {
public int strStr(String source, String target) {
// Write your code here
if(source == null || target == null){
return 0;
}
if(source.length()
注:
- java.lang.String.charAt() 方法返回指定索引处的char值。索引范围是从0到length() - 1。



