[Leetcode][Python]28: Implement strStr()

# -*- coding: utf8 -*-‘‘‘__author__ = ‘[email protected]‘

28: Implement strStr()https://oj.leetcode.com/problems/implement-strstr/

Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

===Comments by Dabay===在长字符串中找子串,当然是KMP算法。既然是三人研究出来的算法,肯定不简单。空了再来研究研究吧。这里给的是直观的解法,遍历长字符串的时候,开始和短字符串一个一个字符进行比较,如果都比较完了,说明找到了。如果遍历完了,还没有返回的话,说明没有找到,返回-1。‘‘‘

class Solution:    # @param haystack, a string    # @param needle, a string    # @return an integer    def strStr(self, haystack, needle):        i = 0        while i < len(haystack) - len(needle) + 1:            m,n = i,0            while n < len(needle) and haystack[m] == needle[n]:                m += 1                n += 1            if n == len(needle):                return i            i += 1        else:            return -1

def main():    sol = Solution()    haystack = "ahi"    needle = "hi"    print sol.strStr(haystack, needle)

if __name__ == "__main__":    import time    start = time.clock()    main()    print "%s sec" % (time.clock() - start)
时间: 2024-10-23 03:01:16

[Leetcode][Python]28: Implement strStr()的相关文章

【一天一道LeetCode】#28. Implement strStr()

一天一道LeetCode系列 (一)题目 Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. (二)解题 第一种解法:朴素匹配算法 /* 两个指针,分别指向两个字符串的首字符 如果相等则一起向后移动,如果不同i取第一个相同字符的下一个开始继续匹配 如果最后j等于needle的长度则匹配成功,返回i-

【LeetCode】28. Implement strStr() 解题小结

题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 简单题. class Solution { public: int strStr(string haystack, string needle) { if (needle.empty()) return 0; if (haystack.em

【LeetCode】28. Implement strStr()

题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 提示: 此题可以使用暴力搜索方法.当然也可以使用其他一些高级方法,如:Rabin-Karp, KMP等等.这些算法打算等之后对他们更了解了以后,单独写一些文章对其总结.因此这里仅给出暴力搜索方法. 代码: class Solution {

44. leetcode 28. Implement strStr()

28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 思路:子串匹配,朴素匹配.

leetCode 28. Implement strStr() 字符串

28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 在haystack中找与needle 第一个相匹配的位置.如果找不到,返回-1. 代码如下: class Solution { public:     int strStr(string haystac

28. Implement strStr()-leetcode-java

[原来在SAE的blog上,都转到CSDN了..] 28. Implement strStr()-leetcode-java 发表于 2016/02/06 题意 Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 字符串匹配问题,如果子串在主串中存在,则返回子串在主串中首次出现的第一个字符的位置,不

28. Implement strStr()【easy】

28. Implement strStr()[easy] Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 解法一: 1 class Solution { 2 public: 3 int strStr(string haystack, string needle) { 4 if (haystack

28. Implement strStr()(js)

28. Implement strStr() Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Example 1: Input: haystack = "hello", needle = "ll" Output: 2 Example 2: Input: haystac

LeetCode 28 Implement strStr() (C,C++,Java,Python)

Problem: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Update (2014-11-02): The signature of the function had been updated to return the index instead of the pointer. If