class Solution {
public:
char *strStr(char *haystack, char *needle) {
if (haystack == NULL || needle == NULL) return NULL;
int wpos[];
char cur = ;
int len = ;
for (int i=; i < ; i++) wpos[i] = -; for (int i=; (cur = needle[i]) != '\0'; i++, len++) wpos[cur] = i;
int p = , q = ;
while (true) {
while (haystack[p] != '\0'
&& needle[q] != '\0'
&& haystack[p] == needle[q]) p++, q++; if ( needle[q] == '\0') {
return haystack + p - q;
} if (haystack[p] == '\0') {
return NULL;
} int npos = p - q + len;
int move = wpos[haystack[npos]];
q = ;
if (move < ) {
p++;
} else {
p = npos - move;
}
}
return NULL;
}
};

又写了次sunday算法,还是磕磕碰碰

第二轮 3.21

Implement strStr().

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

kmp、sunday都想不起来了,直接暴力吧,超时,然后加上一些条件能过应该是testcase不好,否则还是应该能造成超时:

class Solution {
public:
int strStr(char *haystack, char *needle) {
if (haystack == NULL || needle == NULL) {
return -;
} int hlen = strlen(haystack);
int nlen = strlen(needle); if (nlen > hlen) {
return -;
} int hi = ;
int ni = ;
while (hi < (hlen - nlen + )) {
int ti = hi;
ni = ;
// try to campare start from haystack[ti] & needle[ni] (ti=hi, ni=0)
while (needle[ni] != '\0' && haystack[ti] != '\0') {
if (needle[ni] == haystack[ti]) {
ni++, ti++;
} else {
break;
}
}
if (needle[ni] == '\0') {
return hi;
}
// start from next char
hi++;
}
// not found
return -;
}
};

下面就超时:

class Solution {
public:
int strStr(char *haystack, char *needle) {
if (haystack == NULL || needle == NULL) {
return -;
} for (int i=; true; i++) {
int ci = i;
int ni;
for(ni = ; needle[ni] && haystack[ci]; ni++, ci++) {
if (needle[ni] != haystack[ci]) {
break;
}
}
if (needle[ni] == '\0') {
return i;
}
if (haystack[i] == '\0') {
break;
}
} // not found
return -;
}
};

下面又不超时,到底是为什么?不是一样的方式么

class Solution {
public:
int strStr(char *haystack, char *needle) {
int i,j;
for (i = j = ; haystack[i] && needle[j];) {
if (haystack[i] == needle[j]) {
++i;
++j;
} else {
i = i - j + ;
j = ;
}
}
return needle[j] ? - : (i - j);
}
};

一次Sunday算法:

#include <stdio.h>
#include <stdlib.h>
#include <string.h> int sunday(const char* src, const char* pattern) {
if (src == NULL || pattern == NULL) return -1;
int slen = strlen(src);
int plen = strlen(pattern); int cpos = ;
int dict[] = {};
memset(dict, -, sizeof(dict));
for (int i=; i<plen; i++) {
dict[pattern[i]] = i;
} while (cpos + plen <= slen) {
int i = ;
while (i < plen) {
if (src[i+cpos] != pattern[i]) {
break;
}
i++;
} if (i == plen) {
return cpos;
} int move = dict[src[cpos + plen]];
// printf("tail char: %c\n", pattern[cpos + plen]);
if (move < ) {
cpos = cpos + plen;
} else {
cpos = cpos + plen - move;
}
// printf("next pos:%d, %c\n", cpos, src[cpos]);
}
return -;
} int main(int argc, char* argv[]) { if (argc < ) {
printf("%s <src string> <pattern string>\n", argv[]);
return ;
} int idx = sunday(argv[], argv[]);
if (idx < ) {
printf("not found pattern in src string\n");
return ;
}
printf("match idx: %d, string from: %s\n", idx, argv[] + idx);
return ;
}

可以再简化代码:

#include <iostream>
#include <cstdlib> int sunday(const char* pattern, const char* str) {
if (pattern == NULL || str == NULL) {
return -;
}
int slen = , plen = ; while (pattern[plen] != '\0') plen++;
while (str[slen] != '\0') slen++; int tbl[];
for (int i=; i<; i++) tbl[i] = -;
for (int i=; i<plen; i++) tbl[pattern[i]] = i; int pi = , si = ; while (si < slen) {
while (str[si] == pattern[pi] && si < slen) si++, pi++;
if (pi == plen) return si - plen;
int nidx = plen - pi + si;
int offset = tbl[str[nidx]];
si = nidx - offset;
pi = ;
}
return -;
} int main() {
char* pattern = "simple";
char* str = "the example is a simple example."; int idx = sunday(pattern, str);
if (idx < ) {
printf("not found\n");
} else {
printf("found at: %d\n", idx);
printf("str:%s\n", str + idx);
}
system("pause");
return ;
}
si = nidx - offset;
这里当offset是负数时和非负数时其实是两种情况,但是因为将tbl数组初始化了为-1,所以nidx-offset其实相当于nidx + 1,刚刚可以把两种情况统一了。 感觉前面的暴力法写的复杂了,思路可以再清晰一些:
class Solution {
public:
int strStr(string haystack, string needle) {
int hlen = haystack.size();
int nlen = needle.size(); for (int i=; i<hlen; i++) {
int p = i, q = ;
if (hlen - i < nlen) {
return -;
}
while (p < hlen && q < nlen && haystack[p] == needle[q]) p++, q++;
if (q == nlen) {
return i;
}
}
return nlen == ? : -;
}
};

LeetCode ImplementStrstr的更多相关文章

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

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

  2. leetcode算法分类

    利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...

  3. LeetCode题目分类

    利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...

  4. <转>LeetCode 题目总结/分类

    原链接:http://blog.csdn.net/yangliuy/article/details/44514495 注:此分类仅供大概参考,没有精雕细琢.有不同意见欢迎评论~ 利用堆栈:http:/ ...

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

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 28: Implement strStr()https://oj.leetco ...

  6. [LeetCode]题解(python):028-Implement strStr()

    题目来源: https://leetcode.com/problems/implement-strstr/ 题意分析: 输入两个字符串haystack和needle,如果needle是haystack ...

  7. LeetCode 题目总结/分类

    LeetCode 题目总结/分类 利用堆栈: http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ http://oj.l ...

  8. Leetcode 题解

    Leetcode Solutions Language: javascript c mysql Last updated: 2019-01-04 https://github.com/nusr/lee ...

  9. LeetCode 28 Implement strStr() (实现找子串函数)

    题目链接: https://leetcode.com/problems/implement-strstr/?tab=Description   Problem : 实现找子串的操作:如果没有找到则返回 ...

随机推荐

  1. [JS] 瀑布流加载

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name ...

  2. the type initializer for 'system.drawingcore.gdiplus' threw an exception

    Centos 7 yum install libgdiplus-devel reboot之后生效 apt install libgdiplus cp /usr/lib/libgdiplus.so ~/ ...

  3. HDU-1160-FatMouse's Speed(线性DP,LIS)

    FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  4. 使用python 模仿mybinlog 命令 二进制分析mysql binlog

    出处:黑洞中的奇点 的博客 http://www.cnblogs.com/kelvin19840813/ 您的支持是对博主最大的鼓励,感谢您的认真阅读.本文版权归作者所有,欢迎转载,但请保留该声明. ...

  5. 题解51nod1515——明辨是非

    前提 在这道题老师讲过之后,再考时,我还是WA了ヽ(*.>Д<)o゜果然,我还是好菜啊~%?…,# *'☆&℃$︿★? 谢谢Jack_Pei dalao的帮忙.~~O(∩_∩)O~ ...

  6. HLS:OpenCV和RTL代码转换关系

    OpenCV 图像处理是基于存储器帧缓存而构建的, 它总是假设视频帧数据存放在外部 DDR 存储器中. 由于处理器的小容量高速缓存性能的限制, 因此, OpenCV 访问局部图像性能较差. 并且, 从 ...

  7. Mac 10.12安装WebStorm

    下载: (链接: https://pan.baidu.com/s/1c2o8wUG 密码: 2waz)

  8. centos安装图形操作界面

    yum groupinstall "GNOME Desktop" "Graphical Administration Tools"

  9. 装B 自卫神器 -

    IE内核下,使用XX插件 修改网页源代码直接无缝显示数据. 包括https FF同样. 截图: 可以修改所有客户端数据,用以迷惑他们.纯属娱乐 ``````` 再次,顺便提醒下. 做网赚的很多朋友,切 ...

  10. WebDriver+TestNG的一个典型例子

    想让测试更加灵活,1. 可以配置使用任意支持的浏览器进行测试:2. 配置所有Google的URL:3. 配置搜索的关键字.修改后的代码: public class GoogleTest { WebDr ...