28. Implement strStr()

String相等 == 只是比较引用值就是地址
如果是new String的话 例如substring跟其他的比较就要用str.equal()

 

 

 1 class Solution {
 2     public int strStr(String haystack, String needle) {
 3         if(needle == null) return 0;
 4         if(haystack.length() < needle.length()) return -1;
 5         int nlen = needle.length();
 6         int res = -1;
 7         for(int i = 0; i < haystack.length() - nlen + 1; i++) {
 8             if(needle.equals(haystack.substring(i, i+nlen))) {
 9                 return i;
10             }
11         }
12         return res;
13         
14     }
15 }

 

posted @ 2018-09-07 23:06  jasoncool1  阅读(170)  评论(0编辑  收藏  举报