8000 add ImplementstrStr · jiaxinhuang/python-test-code@0596ba6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0596ba6

Browse files
committed
add ImplementstrStr
1 parent d4adf9e commit 0596ba6

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

leetcode/ImplementstrStr.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#-*- coding:utf-8 -*-
2+
'''
3+
Created on 2017年4月19日
4+
5+
@author: huangjiaxin
6+
'''
7+
class Solution(object):
8+
def strStr(self, haystack, needle):
9+
"""
10+
:type haystack: str
11+
:type needle: str
12+
:rtype: int
13+
"""
14+
hlen, nlen = len(haystack), len(needle)
15+
if nlen == 0:
16+
return 0
17+
if hlen == 0 or hlen < nlen:
18+
return -1
19+
for i in range(hlen - nlen + 1):
20+
if haystack[i:i+nlen] == needle:
21+
return i
22+
return -1
23+
24+
25+
if __name__ == '__main__':
26+
haystack = ''
27+
needle = ''
28+
print Solution().strStr(haystack, needle)
29+

0 commit comments

Comments
 (0)
0