Implement strStr()
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
要注意特殊情况,比如strstr("","") 应该等于0 =_=
class Solution:
# @param haystack, a string
# @param needle, a string
# @return an integer
def strStr(self, haystack, needle):
l1=len(haystack)
l2=len(needle)
<span style="color:#ff0000;">if l2==0:
return 0</span>
for i in range(l1-l2+1):
for j in range(l2):
if haystack[i+j]!=needle[j]:
break
if j==l2-1:
return i
return -1