Skip to content

Commit e43ed48

Browse files
committedSep 27, 2016
make code more concise
1 parent 4f7f0bd commit e43ed48

File tree

1 file changed

+3
-10
lines changed

1 file changed

+3
-10
lines changed
 

‎EASY/src/easy/ImplementStrStr.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,9 @@
33
public class ImplementStrStr {
44
/**You could use substring as follows, or use two pointers to go through the haystack, if substring API call is not allowed.*/
55
public static int strStr(String haystack, String needle) {
6-
if(haystack == null || needle == null){
7-
return -1;
8-
}
9-
if(haystack.isEmpty()){
10-
return needle.isEmpty() ? 0 : -1;
11-
}
12-
if(needle.isEmpty()){
13-
return 0;
14-
}
15-
for(int i = 0; i < haystack.length() - needle.length()+1; i++){
6+
if(haystack == null || needle == null || haystack.length() < needle.length()) return -1;
7+
8+
for(int i = 0; i <= haystack.length() - needle.length(); i++){
169
if(haystack.substring(i, i+needle.length()).equals(needle)) return i;
1710
}
1811
return -1;

0 commit comments

Comments
 (0)