http://oj.leetcode.com/problems/implement-strstr/

判断一个串是否为另一个串的子串

比较简单的方法,复杂度为O(m*n),另外还可以用KMP时间复杂度为O(m+n),之前面试的时候遇到过。

class Solution {
public:
bool isEqual(char *a,char *b)
{
char* chPointer = a;
char* chPointer2 = b;
while(*chPointer2!= '\0' && *chPointer!='\0' )
{
if(*chPointer == *chPointer2)
{
chPointer++;
chPointer2++;
}
else
return false;
}
return true;
}
char *strStr(char *haystack, char *needle) {
char* chPointer = haystack;
char* chPointer2 = needle;
if(haystack == NULL || needle ==NULL || haystack =="")
return NULL;
while(chPointer)
{
if(isEqual(chPointer,needle))
return chPointer;
chPointer++;
}
return NULL;
}
};
class Solution {
public:
void compute_prefix(const char* pattern,int next[])
{
int i;
int j = -;
const int m = strlen(pattern);
next[] = j;
for(i =;i<m;i++)
{
while(j>- && pattern[j+] != pattern[i]) j = next[j];
if(pattern[i]== pattern[j+])
j++;
next[i] = j;
}
}
int kmp(const char* text , const char* pattern)
{
int i;
int j = -;
const int n = strlen(text);
const int m = strlen(pattern);
if(n == m && m==)
return ;
if(m == )
return ;
int *next = (int *)malloc(sizeof(int) * m); compute_prefix(pattern, next); for(i = ;i <n;i++)
{
while(j >- && pattern[j+] != text[i])
j = next[j];
if(text[i] == pattern[j+]) j++;
if(j==m-)
{
free(next);
return i-j;
}
}
}
char *strStr(char *haystack, char *needle) {
int position = kmp(haystack,needle);
if(position == -)
return NULL;
else
return (char*)haystack + position;
}
};

LeetCode OJ--Implement strStr()的更多相关文章

  1. Leetcode OJ : Implement strStr() [ Boyer–Moore string search algorithm ] python solution

    class Solution { public: int strStr(char *haystack, char *needle) { , skip[]; char *str = haystack, ...

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

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

  3. [leetcode 27]Implement strStr()

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

  4. Leetcode #28. Implement strStr()

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

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

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

  6. 【leetcode】Implement strStr()

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

  7. Java for LeetCode 028 Implement strStr()

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

  8. Java [leetcode 28]Implement strStr()

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

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

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

  10. 44. leetcode 28. Implement strStr()

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

随机推荐

  1. JdbcTemplate类对sql的操作使用

    <!--方式一: dbcp 数据源配置,在测试环境使用单连接 --> <bean id="dataSource" class="org.apache.c ...

  2. oracle count 百万级 分页查询记要总数、总条数优化

    oracle count 百万级 分页查询记录总数.总条数优化 oracle count 百万级 查询记录总数.总条数优化 最近做一个项目时,做分页时,发现分页查询速度很慢,分页我做的是两次查询,一次 ...

  3. Ukulele 常用和弦

  4. IAP介绍

    iOS应用调置 wjforstudy分享了IAP的一些基本知识.在论坛的地址是:http://www.cocoachina.com/bbs/read.php?tid=92060  1.在开始IAP开发 ...

  5. 661. Image Smoother@python

    Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother t ...

  6. html中注释的问题

    在修改jsp页面的时候遇到了一个有点难懂的注释,mark一下 <script> <!-- alert("js code"); //--> </scri ...

  7. (4)zabbix监控第一台服务器

    2. zabbix监控服务器 创建主机,选择模板以及录入基本信息,过一分钟左右,就可以看到cpu.内存.硬盘等等使用情况.本节以图文为主.by the way, zabbix中文翻译很烂,config ...

  8. mysql:having 用法

    顺序:where -> group by -> min -> order by -> limit 在select语句中使用having 子句来指定一组行或聚合的过滤条件 hav ...

  9. Verilog之delay的两种用法(inter/intra)

    verilog语言中有两种延迟方式:inter-delay和intra-delay,关于inter和intra.这两个英文前缀都有“内部,之间”的意思,但又有所不同.inter表达不同事物之间,int ...

  10. dbfread报错ValueError错误解决方法

    问题 我在用dbfread处理.dbf数据的时候出现了报错 ValueError("could not convert string to float: b'.'",) 然后查找. ...