-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution28.java
More file actions
36 lines (35 loc) · 982 Bytes
/
Solution28.java
File metadata and controls
36 lines (35 loc) · 982 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* Created by Alex on 2016/5/19.
*/
public class Solution28 {
public int strStr(String haystack, String needle) {
char[] haystackChar = haystack.toCharArray();
char[] needleChar = needle.toCharArray();
if(haystackChar.length == 0 && needleChar.length == 0){
return 0;
}
if(haystackChar.length == 0){
return -1;
}
if(needleChar.length == 0){
return 0;
}
for(int i=0; i<=haystackChar.length - needleChar.length; i++){
boolean flag = true;
int index = i;
int result = i;
for(int j=0; j<needleChar.length; j++){
if(haystackChar[index] == needleChar[j]){
index++;
}else {
flag = false;
break;
}
}
if(flag){
return result;
}
}
return -1;
}
}