zoukankan      html  css  js  c++  java
  • [Leetcode][Python]28: Implement strStr()

    # -*- coding: utf8 -*-
    '''
    __author__ = 'dabay.wang@gmail.com'

    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)
  • 相关阅读:
    OO第四次暨期末总结
    OO第九到十一次作业小结
    OO第五到七次作业小结
    OO前三次作业阶段小结
    数据预处理相关
    Latex+VScode安装
    python学习网站+查询网站
    arcgis画图中添加带箭头的直线
    在ArcGIS 中标注中竖排文字
    vs2015使用fopen时遇到unsafe问题
  • 原文地址:https://www.cnblogs.com/Dabay/p/4261429.html
Copyright © 2011-2022 走看看