码迷,mamicode.com
首页 > 其他好文 > 详细

28. Implement strStr()

时间:2018-09-07 23:53:21      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:class   bsp   tac   pre   引用   string   ring   i++   style   

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 }

 

28. Implement strStr()

标签:class   bsp   tac   pre   引用   string   ring   i++   style   

原文地址:https://www.cnblogs.com/goPanama/p/9607505.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!