首页 > 其他 > 详细

[Leetcode] Implement strStr()

时间:2014-10-18 08:33:05      阅读:197      评论:0      收藏:0      [点我收藏+]

Implement strStr().

Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.

Solution:

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

 

[Leetcode] Implement strStr()

原文:http://www.cnblogs.com/Phoebe815/p/4032472.html

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