lintcode-13-字符串查找
字符串查找
对于一个给定的 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-字符串查找的更多相关文章
- lintcode:strStr 字符串查找
题目: 字符串查找 字符串查找(又称查找子字符串),是字符串操作中一个很有用的函数.你的任务是实现这个函数. 对于一个给定的 source 字符串和一个 target 字符串,你应该在 source ...
- LintCode 13. Implement strStr()
LintCode 13. Implement strStr() 题目描述 对于一个给定的 source 字符串和一个 target 字符串,你应该在 source 字符串中找出 target 字符串出 ...
- KMP 算法 & 字符串查找算法
KMP算法 Knuth–Morris–Pratt algorithm 克努斯-莫里斯-普拉特 算法 algorithm kmp_search: input: an array of character ...
- Rabin-Karp指纹字符串查找算法
首先计算模式字符串的散列函数, 如果找到一个和模式字符串散列值相同的子字符串, 那么继续验证两者是否匹配. 这个过程等价于将模式保存在一个散列表中, 然后在文本中的所有子字符串查找. 但不需要为散列表 ...
- 自己动手写文件查找,字符串查找,查询jar包等工具
文件查找——搜索当前目录下的文件 知道大概的文件名称,使用 findf FileName findf.py import argparse, re, os from os.path import jo ...
- 关于字符串查找 charindex ,Patindex 还有一个like
字符串查找.在模糊朝找的情况下,其实3者的效率是差不多的.都需要一个一个取出来然后扫一遍╮(╯_╰)╭.然而用法还是会有一点儿的区别 1 charindex (查找的字符串,字符串表达式[,开始查找的 ...
- python 字符串查找
python 字符串查找有4个方法,1 find,2 index方法,3 rfind方法,4 rindex方法. 1 find()方法: )##从下标1开始,查找在字符串里第一个出现的子串:返回结果3 ...
- Sunday算法(字符串查找、匹配)
字符串查找算法中,最著名的两个是KMP算法(Knuth-Morris-Pratt)和BM算法(Boyer-Moore).两个算法在最坏情况下均具有线性的查找时间.但是在实用上,KMP算法并不比最简单的 ...
- Rabin-Karp字符串查找算法
1.简介 暴力字符串匹配(brute force string matching)是子串匹配算法中最基本的一种,它确实有自己的优点,比如它并不需要对文本(text)或模式串(pattern)进行预处理 ...
- php中常用的字符串查找函数strstr()、strpos()实例解释
string strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] ) 1.$haystack被查找的字 ...
随机推荐
- Debian中CodeIgniter+nginx+MariaDB+phpMyAdmin配置
本文不讲述软件安装过程,记述本人在Debia中配置CodeIgniter时遇到的问题及解决方法,希望能够为有需要的人提供帮助. 一.Debian版本及所需的软件 Debian 9.8 stretch ...
- python逻辑判断 () not and or
python逻辑判断 () not and or 优先级关系:()>not>and>or 运算符示意 not –表示取反运算. and –表示取与运算. or –表示取或运算. or ...
- MATLAB数学实验总结
L1 MATLAB 基础知识 P6 表1-3 数据显示格式 format rat format long P20 表2-5 常用的矩阵函数 zeros(m,n) %零阵 eye(n) %单位阵 one ...
- fedora/centos下gcc编译出现gcc: error trying to exec ‘cc1plus’: execvp: No such file or directory
fedora/centos下gcc编译出现gcc: error trying to exec 'cc1plus': execvp: No such file or directory解决办法 翻译自: ...
- SpringBoot-07:SpringBoot整合PageHelper做多条件分页查询
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 本篇博客讲述如何在SpringBoot中整合PageHelper,如何实现带多个条件,以及PageInfo中的 ...
- 通过反编译看Java String及intern内幕--费元星站长
通过反编译看Java String及intern内幕 一.字符串问题 字符串在我们平时的编码工作中其实用的非常多,并且用起来也比较简单,所以很少有人对其做特别深入的研究.倒是面试或者笔试的时候,往 ...
- 使用PowerDesign15反向生成数据库
在Pd15中建立物理模型后,可以通过反向工程直接生成数据库的表结构.主要有以下几个步骤: 1. 首先设置一下数据库配置,选择对应要使用的数据库(此处选择Sql Server 2008 R ...
- Odd CSS syntax. [class^='icon-'], [class*=' icon-']
原文: https://stackoverflow.com/questions/20322740/odd-css-syntax-class-icon-class-icon I am going thr ...
- C++ 基础面试题-2
请写出一下程序的输出内容 /* ** 2018/03/21 22:02:03 ** Brief: ** Author:ZhangJianWei ** Email:Dream_Dog@163.com * ...
- Linux管道及I/O重定向
I/O: 系统设定 默认输入设备:标准输入,STDIN,0 默认输出设备:标准输出,STDOUT,1 标准错误输出:STDERR,2 属于不同的数据流 标准输入:键盘 标准输出和错误输出:显示器 I/ ...