题目:

  1. class Solution {
  2. public:
  3. int strStr(string haystack, string needle) {
  4. if(needle.empty()){
  5. return 0;
  6. }
  7. vector<int> next;
  8. int i = 0;
  9. int j = 0;
  10. int sLen = haystack.size();
  11. int tLen = needle.size();
  12. next = getNext(needle);
  13. while((i<sLen)&&(j<tLen)){
  14. if((j==-1)||(haystack[i]==needle[j])){
  15. i++;
  16. j++;
  17. }
  18. else{
  19. j = next[j];
  20. }
  21. }
  22. if(j==tLen){
  23. return i-j;
  24. }
  25. return -1;
  26. }
  27. vector<int> getNext(string pattern){
  28. vector<int> next;
  29. int j = -1;
  30. int i = 0;
  31. int len = pattern.size();
  32. next.push_back(-1);
  33. while(i<len-1){
  34. if(j==-1||pattern[i] == pattern[j]){
  35. i++;
  36. j++;
  37. next.push_back(j);
  38. }
  39. else{
  40. j = next[j];
  41. }
  42. }
  43. return next;
  44. }
  45. };

思路:KMP算法,重点在getNext。KMP算法的话打算另外写,这里就不写了。

上面这个是未经过优化的,如果进行优化则:

  1. vector<int> getNext(string pattern){
  2. vector<int> next;
  3. int j = -1;
  4. int i = 0;
  5. int len = pattern.size();
  6. next.push_back(-1);
  7. while(i<len-1){
  8. if(j==-1||pattern[i] == pattern[j]){
  9. i++;
  10. j++;
  11. if(pattern[j]!=pattern[i]){
  12. next.push_back(j);
  13. }
  14. else{
  15. next.push_back(next[j]);
  16. }
  17. }
  18. else{
  19. j = next[j];
  20. }
  21. }
  22. return next;
  23. }

就是当前缀和后缀相同的时候那么应该应该继续递归,因为相同字符没意义。

值得注意的是while((i<sLen)&&(j<tLen)),一定要先将needle.size()haystack.size()赋值给新的变量保存,不然会出错:

LeetCode——28. Implement strStr()的更多相关文章

  1. 44. leetcode 28. Implement strStr()

    28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in ha ...

  2. [LeetCode] 28. Implement strStr() 实现strStr()函数

    Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...

  3. Leetcode #28. Implement strStr()

    Brute Force算法,时间复杂度 O(mn) def strStr(haystack, needle): m = len(haystack) n = len(needle) if n == 0: ...

  4. Java [leetcode 28]Implement strStr()

    题目描述: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...

  5. [LeetCode] 28. Implement strStr() 解题思路

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  6. Leetcode 28——Implement strStr()

    Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...

  7. [leetcode]28. Implement strStr()实现strStr()

    Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...

  8. [LeetCode] 28. Implement strStr() ☆

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  9. LeetCode 28 Implement strStr() (实现找子串函数)

    题目链接: https://leetcode.com/problems/implement-strstr/?tab=Description   Problem : 实现找子串的操作:如果没有找到则返回 ...

随机推荐

  1. vb中typename函数

    适用于获得一个变量的类型名称的, 比如 A 是一个字符串变量,那么 TypeName(A)=String

  2. boost::regex

    https://blog.51cto.com/liam2199/2108548 正则表达式

  3. springboot easyexcel

    pom..xml <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel&l ...

  4. 【BZOJ1488】[HNOI2009]图的同构计数

    题目链接 题意 求 n 个点的同构意义下不同的图的数量.\((n\leq 60)\) Sol \(Polya\) 定理的练手题. 我们这里先把边的存在与否变成对边进行黑白染色,白色代表不存在,这样就变 ...

  5. java:集合输出Iterator,ListIterator,foreach,Enumeration

    //集合输出,集合的四种输出 Iterator, ListIterator, foreach, Enumeration 只要碰到集合,第一输出选择是Iterator类. Iterator<E&g ...

  6. 移动web开发之像素和DPR详解

    前话: 像素在web开发中几乎天天用到,但到底什么是像素,移动端和桌面端的像素有区别吗,缩放对像素有影响吗,视网膜屏幕和像素有什么关系?关于这些问题,可能就不清楚了.本文将介绍关于像素的相关知识 什么 ...

  7. 【GDOI2014模拟】服务器

    前言 直到比赛最后几分钟,才发现60%数据居然是一个水dp,结果没打完. 题目 我们需要将一个文件复制到n个服务器上,这些服务器的编号为S1, S2, -, Sn. 首先,我们可以选择一些服务器,直接 ...

  8. python-登录保持

     cookies.Session import requests url1="http://127.0.0.1:5000/login" url2="http://127. ...

  9. time,sys,os模块

      1.time模块 a:结构化时间:struct_time:通过time.localtime获取到一个时间对象,通过这个对象得到对象属性 ****localtime()如果没有参数,默认返回是一个时 ...

  10. find查找多种文件后缀

    find命令最常用的是查找某个文件,如: find ./ -name "test.txt" 则会在当前目录及子目录下查找test.txt文件 更常用的是查找某一类型的文件,如: f ...