这个题目是典型的KMP算法,当然也可以试试BM,当然有关KMP和BM的介绍阮一峰曾经写过比较好的科普,然后july也有讲解,不过那个太长了。

先放题目吧:

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 you still see your function signature returns a char * or String, please click the reload button to reset your code definition.

就是找到haystack内是否有needle这个字符串,字符串查找算法。

KMP主要是建立一个next的部分匹配数组,来用空间换时间。next的生成函数是KMP关键,解法如下:

class Solution {
public:
vector<int> generateNext(char *str)
{
int len = strlen(str);
vector<int> tmp;
tmp.push_back(0); for (int i = 1, j = 0; i < len; i++)
{
while (j > 0 && str[i] != str[j])
{
j = tmp.at(j - 1);
} if (str[i] == str[j])
{
j++;
} tmp.push_back(j);
} return tmp;
} int strStr(char *haystack, char *needle)
{
vector<int> next = generateNext(needle); int lenH = strlen(haystack);
int lenN = strlen(needle); if (lenN == 0)
{
return 0;
}
if (lenH < lenN)
{
return -1;
}
int i, j; for (i = 0, j = 0; i < lenH; i++)
{
while (j > 0 && haystack[i] != needle[j])
{
j = next[j - 1];
} if (haystack[i] == needle[j])
{
j++;
} if (j == lenN)
{
return i - lenN + 1;
}
}
return -1;
}
};

[leetcode] 21. Implement strStr()的更多相关文章

  1. [LeetCode] 28. Implement strStr() 实现strStr()函数

    Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...

  2. [leetcode 27]Implement strStr()

    1 题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...

  3. Leetcode #28. Implement strStr()

    Brute Force算法,时间复杂度 O(mn) def strStr(haystack, needle): m = len(haystack) n = len(needle) if n == 0: ...

  4. 【leetcode】Implement strStr() (easy)

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

  5. 【leetcode】Implement strStr()

    Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in haysta ...

  6. Java for LeetCode 028 Implement strStr()

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

  7. Java [leetcode 28]Implement strStr()

    题目描述: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...

  8. [LeetCode] 28. Implement strStr() 解题思路

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

  9. 44. leetcode 28. Implement strStr()

    28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in ha ...

随机推荐

  1. knn手写识别

    import numpy as np import operator import os #KNN算法 def knn(k,testdata,traindata,labels):#(k,测试样本,训练 ...

  2. linux 随笔

    LINUX环境下的批处理文件的扩展名是.sh,而在windows环境的批处理文件名是.bat

  3. vmware虚拟机桥接模式不能上网

    方法/步骤     首先我的主机的有线连接是正常的,如下:   但是我的虚拟机的网络连接模式为桥接模式,但是却上不了网,如下:   我们来确认下,我的虚拟机的网络模式,如下:   设置全部都是对的,但 ...

  4. flush(), clear(), save()的简单解释

    hibernate最新发布包的javadoc里对这三个方法的解释是: clear() :Completely clear the session.清空session,该清空操作只对于要保存的.删除的和 ...

  5. Openvpn 日常问题解决

    一.Openven的在windows系统下的使用: 1.Openven客户端2.2.0:http://pan.baidu.com/s/1sjJij4T 安装好客户端软件后,将服务器下发的证书和配置文件 ...

  6. Segments(叉积)

    Segments http://poj.org/problem?id=3304 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: ...

  7. C/C++堆、栈及静态数据区详解

    转自:https://www.cnblogs.com/hanyonglu/archive/2011/04/12/2014212.html  做略微修改 C/C++堆.栈及静态数据区详解   本文介绍C ...

  8. 复杂度分析 quick sort&merge sort

    空间复杂度看新开了什么数据结构就够了 公式=几个点*每个点执行了多少次 二叉树都是n次 二分法查找:lgn 全部查找:n n:找一个数,但是两边都要找.相当于遍历.类似于rotated sorted ...

  9. cmake 总结

    cmake中一些预定义变量 PROJECT_SOURCE_DIR 工程的根目录 PROJECT_BINARY_DIR 运行cmake命令的目录,通常是${PROJECT_SOURCE_DIR}/bui ...

  10. memcache缓存失效

    缓存过期 memcached在处理过期的缓存项时,采用懒惰模式处理方法. 缓存项过期,不会立即删除,直到对该缓存项执行了get操作,才会删除过期缓存. > set key 0 10 > t ...