首页 > 其他 > 详细

28. Implement strStr()

时间:2018-09-07 23:38:22      阅读:234      评论:0      收藏:0      [点我收藏+]

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()

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!