题目描述:
Implement strStr().
Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Example 1:
Input: haystack = "hello", needle = "ll" Output: 2
Example 2:
Input: haystack = "aaaaa", needle = "bba" Output: -1
Clarification:
What should we return when needle
is an empty string? This is a great question to ask during an interview.
For the purpose of this problem, we will return 0 when needle
is an empty string. This is consistent to C's strstr()and Java's indexOf().
代码解答:
package com.jack.algorithm;
/**
* create by jack 2018/11/5
*
* @author jack
* @date: 2018/11/5 21:59
* @Description:
* 查找指定字符串在字符串的位置
*/
public class ImplementStrstr {
/**
* 题目描述:
* https://leetcode.com/problems/implement-strstr/
* @param haystack
* @param needle
* @return
*/
public static int strStr(String haystack, String needle) {
if (needle.isEmpty()) {
return 0;
}
int i=0;
int index = -1;
for (;i<haystack.length();i++) {
if (haystack.charAt(i) == needle.charAt(0)) {
int t=i;
int j=0;
while (j < needle.length()) {
if (t<haystack.length() && needle.charAt(j) == haystack.charAt(t)) {
j++;
t++;
} else {
break;
}
}
if (j == needle.length()) {
index = i;
break;
} else {
index = -1;
}
}
}
return index;
}
public static void main(String[] args) {
//String haystack = "hello";
String haystack = "aaaaa";
//String needle = "ll";
String needle = "bba";
int index = strStr(haystack,needle);
System.out.println("index = "+index);
}
}
源码地址: