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 : 实现找子串的操作:如果没有找到则返回 ...
随机推荐
- RN 47 初始化 Bridge 过程
- (instancetype)initWithDelegate:(id<RCTBridgeDelegate>)delegate bundleURL:(NSURL *)bundleURL ...
- 48.rocketMQ
一.简介 RocketMQ是阿里旗下的一款产品,分为开源版本和非开源版本.相比于ActiveMQ,RocketMQ支持顺序消费.事务机制.失败重试机制.消息可查询.消息订阅.较强的水平扩展能力.亿级堆 ...
- Java操作数据库实现"增删改查"
本文主要讲解JDBC操作数据库 主要实现对MySql数据库的"增删改查" 综合概述: JDBC的常用类和接口 一 DriverManager类 DriverManage类 ...
- [BZOJ 2894]世界线
传送门 \(\color{green}{solution}\) 在开这道题之前建议先看看3756:pty的字符串,然后你会发现这题就很zz了. 当然,作为一名合格的博主,我还是应该写点什么的 首先,我 ...
- 【性能调优】:记录一次数据库sql语句性能调优过程
一,依旧很简单的一个接口,查询列表接口,发现10并发单交易场景下,数据库表4w铺底数据,每次查询2000条数据进行orderby显示,平均响应时间2秒以上,数据库的cpu使用率高达95%: 二,抓到这 ...
- Mac下使用crontab来实现定时任务
说明: 1.Linux和Mac下操作crontab都是一致的 2.配置文件都在/etc/crontab下,如果没有就创建. 3.测试发现直接使用crontab -e命令创建的定时任务是放在临时文件夹的 ...
- Hibernate3.3.2_JUnit_BoforeClass不报异常的Bug处理
假如你把配置文件写错了,myeclipse竟然不报错,只说sf空指针. <mapping class="com.oracle.hibernate.model."/> / ...
- 第2章—装配Bean—通过java代码装配bean
通过java代码装配bean 在进行显式装配的时候,有两种选型方案:java和XML配置,这里先介绍java的配置方式. 2.3.1创建配置类 先复习下上一章的配置内容: @Configurati ...
- vertical-aligin
垂直对齐元素只应用于行内元素(图像.文本)和表单元素 垂直对齐文本会影响行高 默认值为baseline
- BUG~JS
2017-11-06 1.没想到啊,这么久了,居然会有这种错误.canvas绘制图片,图片路径出错.drawImage() 解决方法:测试各参数,不单单是要打印出来,还要注意打印的参数是否为有效值