字符串查找

对于一个给定的 source 字符串和一个 target 字符串,你应该在 source 字符串中找出 target 字符串出现的第一个位置(从0开始)。如果不存在,则返回 -1。

说明

在面试中我是否需要实现KMP算法?

不需要,当这种问题出现在面试中时,面试官很可能只是想要测试一下你的基础应用能力。当然你需要先跟面试官确认清楚要怎么实现这个题。

样例

如果 source = "source" 和 target = "target",返回 -1。

如果 source = "abcdabcdefg" 和 target = "bcd",返回 1。

挑战

O(n2)的算法是可以接受的。如果你能用O(n)的算法做出来那更加好。(提示:KMP)

标签

基本实现 字符串处理 脸书

方法一,暴力破解

class Solution {
public:
/**
* Returns a index to the first occurrence of target in source,
* or -1 if target is not part of source.
* @param source string to be scanned.
* @param target string containing the sequence of characters to match.
*/
int strStr(const char *source, const char *target) {
// write your code here
if(source == NULL || target == NULL)
return -1;
if(source[0] == '\0' && target[0] == '\0')
return 0;
if(target[0] == '\0')
return 0;
int sourceLen = strlen(source), targetLen = strlen(target);
int i=0, j=0;
if (sourceLen < targetLen)
return -1; while(i < sourceLen) {
if(source[i] == target[j]) {
i++;
j++;
}
else {
i = i-j+1;
j = 0;
}
if(target[j] == '\0')
return i-j;
}
return -1;
}
};

方法二:KMP算法

class Solution {
public:
/**
* Returns a index to the first occurrence of target in source,
* or -1 if target is not part of source.
* @param source string to be scanned.
* @param target string containing the sequence of characters to match.
*/
int strStr(const char *source, const char *target) {
// write your code here
if(source == NULL || target == NULL)
return -1;
if(source[0] == '\0' && target[0] == '\0')
return 0;
if(target[0] == '\0')
return 0; int sourceLen = strlen(source), targetLen = strlen(target);
int *next = getNext(target, targetLen); int i=0, j=0;
for (i=0; i<sourceLen; i++) {
while (j > 0 && source[i] != target[j])
j = next[j]; if (source[i] == target[j])
j++; if (j == targetLen) {
return i-j+1;
j = next[j];
}
}
return -1;
} int *getNext(const char *target, int targetLen) {
int *next = new int[targetLen+1];
int i=0, j=0; next[0] = next[1] = 0; for(i=1; i<targetLen; i++) {
while(j>0 && target[i]!=target[j])
j = next[j];
if(target[i] ==target[j])
j++;
next[i+1] = j;
} return next;
}
};

lintcode-13-字符串查找的更多相关文章

  1. lintcode:strStr 字符串查找

    题目: 字符串查找 字符串查找(又称查找子字符串),是字符串操作中一个很有用的函数.你的任务是实现这个函数. 对于一个给定的 source 字符串和一个 target 字符串,你应该在 source ...

  2. LintCode 13. Implement strStr()

    LintCode 13. Implement strStr() 题目描述 对于一个给定的 source 字符串和一个 target 字符串,你应该在 source 字符串中找出 target 字符串出 ...

  3. KMP 算法 & 字符串查找算法

    KMP算法 Knuth–Morris–Pratt algorithm 克努斯-莫里斯-普拉特 算法 algorithm kmp_search: input: an array of character ...

  4. Rabin-Karp指纹字符串查找算法

    首先计算模式字符串的散列函数, 如果找到一个和模式字符串散列值相同的子字符串, 那么继续验证两者是否匹配. 这个过程等价于将模式保存在一个散列表中, 然后在文本中的所有子字符串查找. 但不需要为散列表 ...

  5. 自己动手写文件查找,字符串查找,查询jar包等工具

    文件查找——搜索当前目录下的文件 知道大概的文件名称,使用 findf FileName findf.py import argparse, re, os from os.path import jo ...

  6. 关于字符串查找 charindex ,Patindex 还有一个like

    字符串查找.在模糊朝找的情况下,其实3者的效率是差不多的.都需要一个一个取出来然后扫一遍╮(╯_╰)╭.然而用法还是会有一点儿的区别 1 charindex (查找的字符串,字符串表达式[,开始查找的 ...

  7. python 字符串查找

    python 字符串查找有4个方法,1 find,2 index方法,3 rfind方法,4 rindex方法. 1 find()方法: )##从下标1开始,查找在字符串里第一个出现的子串:返回结果3 ...

  8. Sunday算法(字符串查找、匹配)

    字符串查找算法中,最著名的两个是KMP算法(Knuth-Morris-Pratt)和BM算法(Boyer-Moore).两个算法在最坏情况下均具有线性的查找时间.但是在实用上,KMP算法并不比最简单的 ...

  9. Rabin-Karp字符串查找算法

    1.简介 暴力字符串匹配(brute force string matching)是子串匹配算法中最基本的一种,它确实有自己的优点,比如它并不需要对文本(text)或模式串(pattern)进行预处理 ...

  10. php中常用的字符串查找函数strstr()、strpos()实例解释

    string strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] ) 1.$haystack被查找的字 ...

随机推荐

  1. MapReduce清洗日志数据统计PV量

    package mapreduce.webpv; import java.io.IOException; import org.apache.commons.lang.StringUtils; imp ...

  2. H5测试(转载)

    可能有些朋友不明白啥是H5,但其实生活中我们经常会碰到. 比如,你经常收到的朋友虐狗第一式—结婚请贴. 你的朋友圈,可能会经常看到宝妈们虐狗第二式—晒可爱宝宝的相册. 你有可能也收到过这样,非常直观, ...

  3. linux3.4.2之DMA驱动完整程序

    /* *参考arch/arm/mach-s3c24xx/dma-s3c2410.c */ #include <linux/module.h> #include <linux/kern ...

  4. STM32_3 时钟初始化分析

    在startup文件中,调用了2个函数,一个是System_Init, 另一个是main. System_Init()在system_stm32f10x.c 这个文件中,先看一下时钟树,再分析一下这个 ...

  5. 顺序表删除值为 x 的元素

    思路: 不要去考虑删除的字眼,要考虑如何进行保存非 x 的值 这里提供两种解法,殊途同归: 1.将其中非 x 的元素统计并保存 2.统计为 x 的元素个数,并将非 x 的元素保存 注意事项: 注意这里 ...

  6. C# 使用cmd

    public static string cmd(String command) //向cmd()传入命令行,传入"exit"则退出cmd.exe. { Process p = n ...

  7. 使用gogs和glide来轻松拉取golang第三方库

    golang的第三方代码拉取一直是让人头疼的问题,在github托管的代码还好,托管在其他网站上的代码总会由于大家都懂的原因,无法访问.纵使是github,在拉取文件数量较多的库时,也是比较慢的. 有 ...

  8. 浅谈fail-fast机制

    fail-fast机制即为快速失败机制,个人认为是一种防护措施,在集合结构发生改变的时候,使尽全力抛出ConcurrentModificationException,所以该机制大部分用途都是用来检测B ...

  9. 优步UBER司机全国各地奖励政策汇总 (2月8日-2月14日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  10. VINS(八)初始化

    首先通过imu预积分陀螺仪和视觉特征匹配分解Fundamental矩阵获取rotationMatrix之间的约束关系,联立方程组可以求得外参旋转矩阵: 接下来会检测当前frame_count是否达到W ...