暴力解法(O(mn)):

 1 class Solution {
 2 public:
 3     /**
 4      * Returns a index to the first occurrence of target in source,
 5      * or -1  if target is not part of source.
 6      * @param source string to be scanned.
 7      * @param target string containing the sequence of characters to match.
 8      */
 9     int strStr(const char *source, const char *target) {
10         // write your code here
11         if (!source || !target) return -1;
12         int m = strlen(source), n = strlen(target);
13         for (int i = 0; i < m - n + 1; i++) {
14             int j = 0;
15             for (; j < n; j++)
16                 if (source[i + j] != target[j])
17                     break;
18             if (j == n) return i;
19         }
20         return -1;
21     }
22 };

KMP(O(m + n)):

 1 class Solution {
 2 public:
 3     /**
 4      * Returns a index to the first occurrence of target in source,
 5      * or -1  if target is not part of source.
 6      * @param source string to be scanned.
 7      * @param target string containing the sequence of characters to match.
 8      */
 9     int strStr(const char *source, const char *target) {
10         // write your code here
11         if (!source || !target) return -1;
12         int m = strlen(source), n = strlen(target);
13         if (!n) return 0;
14         vector<int> lps = kmpProcess(target);
15         for (int i = 0, j = 0; i < m; ) {
16             if (source[i] == target[j]) {
17                 i++;
18                 j++;
19             }
20             if (j == n) return i - j;
21             if (i < m && source[i] != target[j]) {
22                 if (j) j = lps[j - 1];
23                 else i++;
24             }
25         }
26         return -1;
27     }
28 private:
29     vector<int> kmpProcess(const char* target) {
30         int n = strlen(target);
31         vector<int> lps(n, 0);
32         for (int i = 1, len = 0; i < n; ) {
33             if (target[i] == target[len])
34                 lps[i++] = ++len;
35             else if (len) len = lps[len - 1];
36             else lps[i++] = 0;
37         }
38         return lps;
39     }
40 };

[LintCode] 字符串查找相关推荐

  1. LintCode 字符串查找

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

  2. 【字符串】字符串查找 ( Rabin-Karp 算法 )

    文章目录 一.字符串查找 二.Rabin-Karp 算法 一.字符串查找 算法题目链接 : https://www.lintcode.com/problem/13/ 在 一个字符串 中查找 另外一个字 ...

  3. 【字符串】字符串查找 ( 蛮力算法 )

    文章目录 一.字符串查找 二.蛮力算法代码示例 一.字符串查找 算法题目链接 : https://www.lintcode.com/problem/13/ 在 一个字符串 中查找 另外一个字符串 第一 ...

  4. cmd html 查找汉子字,字符串查找 cmd find命令

    字符串查找 cmd find命令 2009-07-05 14:33:02 作者: 当我要查找字符串的时候,第一个想到的命令,并不是我在本文中要讲解的find,而是比它更强悍的命令findstr. 我对 ...

  5. python3 字符串查找 效率比较

    Python中字符串查找方式有多种,常见的有re.match/search or str.find 用一个例子来说明各种方式的效率如下: from timeit import timeit impor ...

  6. python字符串find函数-python字符串查找函数的用法详解

    python字符串查找函数的使用 打开Python开发工具IDLE,新建"findstr.py'文件,并写代码如下: s ='/ab/bx,.s' print (s.find('/x')) ...

  7. 查找字符位置_学习VB编程第69天 字符串查找与替换

    今天学习了VB提升教程4期,学习的主要内容是字符串查找与替换.零基础学VB课程已经结束,但是感觉还是没有太大进步,而且目前也没有确定后续该学什么语言,干脆就继续再学一段时间VB吧反正计划的三个月时间还 ...

  8. KMP 算法并非字符串查找的优化 [转]

    算法书和数据结构书对 KMP算法多有介绍,称只需对字符串扫描一遍不需回溯云云 .然而 ,它恐怕只应该作为一种思想存在 ;用于实际的字符串查找并不理想 .要费劲心血实现和优化它 ,才能在特定的字符串上略 ...

  9. 1.14 字符串查找(3种方法)indexOf(), lastlndexOf(), charAt()

    在给定的字符串中查找字符或字符串是比较常见的操作.字符串查找分为两种形式:一种是在字符串中获取匹配字符(串)的索引值,另一种是在字符串中获取指定索引位置的字符. 根据字符查找 String 类的 in ...

最新文章

  1. SAP QM初阶之取样策略如何确定检验批Sample Size?
  2. Vue前后台数据交互实例演示,使用axios传递json字符串、数组
  3. Codeforces936C. Lock Puzzle
  4. jvm(13)-线程安全与锁优化
  5. 计算机网络之传输层:2、UDP协议
  6. 《天天数学》连载25:一月二十五日
  7. css3响应式布局设计——回顾
  8. 漫话:如何给女朋友解释什么是 3PC?
  9. oracle call 存储过程 带out_详解oracle数据库存储过程调试方法
  10. centos7恢复mysql数据库_MySQL数据库之Centos7.3,mysql5.7环境,数据存储空间加大调整方案...
  11. Android常用组件
  12. 常用传感器讲解九--雨滴传感器
  13. vc2005编译出来的程序实现绿色版,即无须安装运行库
  14. Spring Bean生命周期,好像人的一生。。
  15. 我国第一部机载脉冲火控雷达研制历程
  16. mysql物理备份恢复搭建从库_RDS FOR MYSQL 各版本利用物理备份搭建从库方法
  17. mac系统恢复服务器上的安装器损坏,Mac提示App已损坏你应该将它移到废纸篓的解决方案-mac无法安装软件解决教程 - 河东软件园...
  18. 如何设置Ubuntu 自动待机时间
  19. Qt与设计模式(全)
  20. IDEA快捷键、格式和代码规范插件

热门文章

  1. python爬百度翻译-python爬虫实现百度翻译
  2. 为什么说python是世界上最好的语言-Python是世界上最好的语言 吗?
  3. 在windows上的git bash中安装tree 和 linux tree命令使用
  4. python使用os和shutil模块进行文件创建,删除,移动,复制,重命名
  5. 写出现代计算机网络的三个方面的应用,《计算机应用基础》统考模拟试题一及参考答案0...
  6. DropZone(文件上传插件)
  7. 圣杯布局与双飞翼布局全解
  8. pc端常见布局样式总结(针对常见的)
  9. log_archive_dest_1设置报错
  10. 新一代(New Generation)测试框架TestNG