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被查找的字 ...
随机推荐
- angularjs脏机制
Angular 每一个绑定到UI的数据,就会有一个 $watch 对象. watch = { name:'', //当前的watch 对象 观测的数据名 getNewValue:function($s ...
- Mongodb从库配置
1. 先以master方式启动mongodb 2. 导入主库的数据文件:/data/mongodb-3.0.12/bin/mongorestore /data/tmp/mongodbbak/ 3. 关 ...
- Python知乎热门话题爬取
本例子是参考崔老师的Python3网络爬虫开发实战写的 看网页界面: 热门话题都在 explore-feed feed-item的div里面 源码如下: import requests from py ...
- Blender2.79建模快捷键
快捷键 基本操作 滚动鼠标中键滚轮:视图放大或缩小 按住鼠标中键滚轮:视图旋转 单独鼠标右键:选择物体 单独鼠标右键:放置物体 shift+鼠标中键:视图平移 小键盘数字1:前视图:ctrl+1:后视 ...
- c语言实现通讯录管理系统(c课程设计)
工具:Visual C++6.0 说明: 本系统基于C语言实现班级通讯录管理系统,为大一时学习C语言刚入门所做的课程设计.功能包括增.删.查.改等,非常适合初学者练手.通讯录包括的个人信息有姓名.学号 ...
- 笔记-sql语句
笔记-sql语句 1. sql语句基础 虽然经常使用sql语句,但没有一个整体式的文档,整理了一下. 1.1. select foundation: select <colnum ...
- java 关键字super和this
super关键字 作用:调用父类的构造器 只能出现在子类的构造其中,并且必须是第一行 super()中的参数,决定了调用父类的那个构造器 注:如果子类构造器中没有出现super,则默认加上super( ...
- VINS(二)Feature Detection and Tracking
系统入口是feature_tracker_node.cpp文件中的main函数 1. 首先创建feature_tracker节点,从配置文件中读取信息(parameters.cpp),包括: ROS中 ...
- 华硕N43sl VNP 连接问题 800 807 621
使用VPN 创建连接,在我自己的电脑上死活连接不上,换到别人的电脑就是可以妥妥的连接. 换了几多个IP都是800错误,经过测试都不能连接.于是开始排查,把防火墙关闭,把杀毒软件关闭, 在开始命令 输入 ...
- 苏醒的巨人----CSRF
一.CSRF 跨站请求伪造(Cross-Site Request Forgery,CSRF)是指利用 受害者尚未失效的身份认证信息(cookie.会话等),诱骗其点 击恶意链接或者访问包含攻击代码的页 ...