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被查找的字 ...
随机推荐
- JZOJ 4273. 【NOIP2015模拟10.28B组】圣章-精灵使的魔法语
4273. [NOIP2015模拟10.28B组]圣章-精灵使的魔法语 (File IO): input:elf.in output:elf.out Time Limits: 1000 ms Mem ...
- 第6章 HDFS HA配置
目录 6.1 hdfs-site.xml文件配置 6.2 core-site.xml文件配置 6.3 启动与测试 6.4 结合ZooKeeper进行自动故障转移 在Hadoop 2.0.0之前,一个H ...
- 动态链接库函数内的静态变量,奇妙的UNIQUE Bind
title: 动态链接库函数内的静态变量,奇妙的UNIQUE Bind date: 2018-09-28 09:28:22 tags: --- 介绍 模板函数和内敛函数中的静态变量,在跨so中的表现, ...
- centos系统安装后无法稳定连接wifi的解决方法
在安装双系统的时候遇到的问题,虽然不知道原理,但是弄好能用就可以,这类bug太邪恶了 wifi不能用的情况: 先查看wifi状态: rfkill list all 0: hci0: Bluetooth ...
- WebRTC中Android Demo中的远程视频流的获取到传输
1.CallActivity#onCreate 执行startCall开始连接或创建房间 2.WebSocketClient#connectToRoom 请求一次服务器 3.回调到CallActivi ...
- CF813D Two Melodies(dp)
题面 luogu Codeforces 题目大意: 给一个长度为\(n\)的序列,求两个不相交的子集长度之和最大是多少,能放入同一子集的条件是首先顺序不能变,然后每一个相邻的要么相差\(1\)或者相差 ...
- ROS Twist和Odometry消息类型使用(Python)
消息类型: 1. Twist - 线速度角速度 通常被用于发送到/cmd_vel话题,被base controller节点监听,控制机器人运动 geometry_msgs/Twist geometry ...
- NB-IOT使用LWM2M移动onenet基础通信套件对接之APN设置
1. 先搞懂APN是做什么的?APN指一种网络接入技术,是通过手机上网时必须配置的一个参数,它决定了手机通过哪种接入方式来访问网络.对于手机用户来说,可以访问的外部网络类型有很多,例如:Interne ...
- Express 总结
Express Express提供了一个轻量级模块,把nodejs的http功能封装在一个简单易用的接口中.Express也扩展了http模块的功能,能轻松处理服务器的路由.响应.cookie和HTT ...
- 网易云易盾与A10 Networks达成战略合作 携手打造抗DDoS攻击的解决方案
欢迎访问网易云社区,了解更多网易技术产品运营经验. 2018年9月,网易云易盾宣布,与智能和自动化网络安全解决方案提供商A10 Networks结成战略合作伙伴关系.双方将在抗DDoS攻击领域展开深入 ...