[Leetcode] Implement strStr()

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 }
原文地址:https://www.cnblogs.com/Phoebe815/p/4032472.html