Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Python:
class Solution(object):
def strStr(self, source, target):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
# 调用内置函数
#if source is None or target is None:
# return -1
#return source.find(target)
# 两重循环(lintcode并没有AC)
if source is None or target is None:
return -1
len_s = len(source)
len_t = len(target)
for i in range(len_s - len_t + 1):
j = 0
while (j < len_t):
if source[i + j] != target[j]:
break
j += 1
if j == len_t:
return i
return -1