LeetCode ImplementStrstr
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的更多相关文章
- [LeetCode] Implement strStr() 实现strStr()函数
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- leetcode算法分类
利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...
- LeetCode题目分类
利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...
- <转>LeetCode 题目总结/分类
原链接:http://blog.csdn.net/yangliuy/article/details/44514495 注:此分类仅供大概参考,没有精雕细琢.有不同意见欢迎评论~ 利用堆栈:http:/ ...
- [Leetcode][Python]28: Implement strStr()
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 28: Implement strStr()https://oj.leetco ...
- [LeetCode]题解(python):028-Implement strStr()
题目来源: https://leetcode.com/problems/implement-strstr/ 题意分析: 输入两个字符串haystack和needle,如果needle是haystack ...
- LeetCode 题目总结/分类
LeetCode 题目总结/分类 利用堆栈: http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ http://oj.l ...
- Leetcode 题解
Leetcode Solutions Language: javascript c mysql Last updated: 2019-01-04 https://github.com/nusr/lee ...
- LeetCode 28 Implement strStr() (实现找子串函数)
题目链接: https://leetcode.com/problems/implement-strstr/?tab=Description Problem : 实现找子串的操作:如果没有找到则返回 ...
随机推荐
- C#取得控制台应用程序的根目录方法
如有雷同,不胜荣幸,若转载,请注明 取得控制台应用程序的根目录方法1:Environment.CurrentDirectory 取得或设置当前工作目录的完整限定路径2:AppDomain.Curren ...
- php防止网站被刷新
在实际应用中,总会遇到某些页面被恶意用户刷新.当你的系统在某些模块没有使用缓存的时候,频繁的刷新会导致数据库吃紧.下面附上一段代码,防止频繁的刷新造成的死机情况. 主要是从 session方面进行限制 ...
- J2SE基本安装和java的环境变量
J2SE基本安装和java的环境变量 1. 首先登录http://www.oracle.com,下载JDK(J2SE) JDK有很多版本其中JDK 1.0,1.1,1.2,1.3,1.4 1.5 ...
- Hibernate 连接数据库,数据库返回数据超过限制报错
1.packet for query is too large 1024 >. you can change this value on the server mysql max_allowed ...
- 基于python的几种排序算法的实现
#!usr/bin/python3 # -*- coding: utf-8 -*- # @Time : 2019/3/28 10:26 # @Author : Yosef-夜雨声烦 # @Email ...
- [JZOJ6075]【GDOI2019模拟2019.3.20】桥【DP】【线段树】
Description N,M<=100000,S,T<=1e9 Solution 首先可以感受一下,我们把街道看成一行,那么只有给出的2n个点的纵坐标是有用的,于是我们可以将坐标离散化至 ...
- Python离线安装依赖包
1.制作requirement.txt pip freeze > requirement.txt 2.下载离线Pytho安装包 pip download -r requirement.txt - ...
- hibernate多对多单向关联
多对多单向,一个学生可以有多个老师,一个老师也可以教多个学生.老师可以找到他的学生,学生找不到教他的老师. 老师类Teacher: package com.oracle.hibernate; impo ...
- hibernate_boolean类型的处理
xml方式,直接写就行,hibernate会直接帮你生成: javaBean代码片段: private boolean leaf; public boolean isLeaf() { return l ...
- Microsoft Power BI Desktop概念学习系列之Microsoft Power BI Desktop的下载和安装(图文详解)
不多说,直接上干货! 官网 https://powerbi.microsoft.com/zh-cn/downloads/ 这里,一般用126邮箱. 因为对于163这样的邮箱是不行. 欢迎大家,加入我的 ...