这个题目是典型的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. webdriver屏幕截图(python)

    webdriver对当前页面进行截图,截取的是当前页面的全图,不论页面有多长,有两种截图方法 1.get_screenshot_as_file(XXX) 2.save_screenshot(XXX) ...

  2. 刚刚明白了for循环写三角形

    for(int a = 15; a >=1; a--) { for(int b = a - 1; b >=0; b--) { System.out.print("A") ...

  3. centos 网卡聚合及Cisco交换机链路聚合

    一.配置环境 centos 系统.网卡1口和2口做链路聚合.    交换机网口 6口和7口. 二.服务器操作步骤 centos 6 1.创建一个channel bonding interface #v ...

  4. jQuery对象转换为DOM对象(转)

    jQuery对象转换为dom对象 只有jQuery对象才能调用jQuery类库的各种函数,同样有些dom对象的属性和方法在jQuery上也是无法调用的,不过基本上jQuery类库提供的函数包含了所有的 ...

  5. 合并区间 · Merge Intervals & 插入区间 · Insert Interval

    [抄题]: 给出若干闭合区间,合并所有重叠的部分. 给出的区间列表 => 合并后的区间列表: [ [ [1, 3], [1, 6], [2, 6], => [8, 10], [8, 10] ...

  6. Swift4 - 动态计算UITableView中tableHeaderView的高度 - 获取子控件高度和宽度

    核心 : /// 获取 子控件高度 func sizeHeaderToFit(view:UIView) { view.setNeedsLayout() view.layoutIfNeeded() le ...

  7. Ubuntu下多个版本OpenCV管理(Multiple Opencv version)

    背景: 最近,在Nvidia的GPU嵌入式开发板Jetson TX1(简称TX1)上移植深度学习目标检测算法YOLO.在TX1上安装了官方提供的opencv版本——OpenCV4Tegra(OpenC ...

  8. javascript正则表达式验证密码(必须含数字字符特殊符号,长度4-16位之间)

    var newpwd = $("#newpassword").val(); //var pattern = "([A-Za-z]|[0-9]|-|_){4,16}&quo ...

  9. IIS文件名解析漏洞扼要分析

    概括: 从技术角度分析IIS6文件名解析漏洞的原理与IIS7的相关情况. a.IIS6错误解析文件类型现象 1.当WEB目录下,文件名以 xxx.asp;xxx.xxx 来进行命名的时候,此文件将送交 ...

  10. geoserver 文件系统

    我介绍了GeoServer的一些重要的资源以及它们的访问接口,现在来看看它们的保存形式.GeoServer的数据没有保存到数据库,而是文件系统,这让我们的学习轻松不少.默认情况下,GeoServer的 ...