C++ 写leetcode遇到的一些问题总结Implement strStr()
来源:互联网 发布:linux ssh登录时间 编辑:程序博客网 时间:2024/05/14 09:20
KMP算法真是看了很久很久,看很简单,关键是自己写起来还得好好理解理解
个人觉得KMP算法重点要掌握i,j,next的含义,啥也不说了,看了两天,终于调处来了,直接上代码吧。。。。。
代码:
运行时间:4ms
class Solution {public: int strStr(string haystack, string needle) { if(haystack == "" && needle == "") return 0; if(haystack == "" && needle != "") return -1; if(haystack != "" && needle == "") return 0; int i = 0; int j = 0; int HLen = haystack.length(); int NLen = needle.length(); /*next[i]匹配方式:如果next[i] = -1则目标串pD向右移一位,同时模式串指针pP移到0;如果next[i] != -1则目标串pD不变,同时模式串指针pP移到next[i];*/ vector<int> next(NLen, 0); getNext(needle, next); while(i < HLen && j < NLen){ if(haystack[i] == needle[j]){ if(j == NLen) return i - NLen + 1; i++; j++; } else{ if(next[j] == -1){ j = 0; i++; } else{ j = next[j]; } } } if(j == NLen) return i - NLen; else return -1; }private://网上看到的KMP算法,比较简洁,但是不是特别好理解/* void getNext(string pattern, vector<int> &next){ int j = -1; //标记现在的匹配位置 int i = 0; //next下标 int sLen = pattern.length(); next[0] = -1; if (sLen == 1) return; while(i < sLen - 1){ if(j == -1 || pattern[i] == pattern[j]){ i++; j++; next[i] = pattern[i] != pattern[j] ? j : next[j]; } else{ j = next[j]; } } }*/ //自己经过理解之后写的KMP算法,代码不是很简洁,但是比容易理解 void getNext(string pattern, vector<int> &next){ int j = 0; //标记现在的匹配位置 int i = 1; //next下标 int sLen = pattern.length(); /*next[i]匹配方式: 如果next[i] = -1则目标串pD向右移一位,同时模式串指针pP移到0; 如果next[i] != -1则目标串pD不变,同时模式串指针pP移到next[i]; */ next[0] = -1; //如果当前不匹配,那么下一个要找的匹配键值next[k]; if (sLen == 1) return; while (i < sLen){ if (j == -1){ j++; next[i] = pattern[i] == pattern[j] ? next[j] : j; i++; } else{ if (pattern[i] != pattern[j]){ next[i] = j; i++; j = next[j]; //说明到此为止,能够重复的字符应该往回走了 } else{ next[i] = next[j]; i++; j++; } } } }};
0 0
- C++ 写leetcode遇到的一些问题总结Implement strStr()
- [C++]LeetCode: 27 Implement strStr()
- 【leetcode c++】28 Implement strStr()
- 【C++】【LeetCode】28. Implement strStr()
- Leetcode c语言-Implement strStr()
- Leetcode之Implement strStr() 问题
- [LeetCode] 028. Implement strStr() (Easy) (C++/Python)
- LeetCode 之 Implement strStr() — C 实现
- [leetcode-28]implement strstr()(C语言)
- 【LeetCode算法练习(C++)】Implement strStr()
- C++ 写leetcode遇到的一些问题总结string to integer
- C++ 写leetcode遇到的一些问题总结Reverse Words in a String
- C++ 写leetcode遇到的一些问题总结Restore IP Addresses
- 【Leetcode】:28. Implement strStr()问题 in JAVA
- [C++]LeetCode 28: Implement strStr() (实现strStr()函数)
- 【LeetCode】005 Implement strStr() 函数strStr()的功能实现
- LeetCode: Implement strStr()
- [Leetcode] Implement strstr()
- jquery 中json数组的操作 增删改
- TP 配置项及说明
- 如何提高SAT文章阅读速度?
- Android WebView先加载网页文字,后加载图片
- 颜色对照表
- C++ 写leetcode遇到的一些问题总结Implement strStr()
- 对内存的思考
- hdu 1166 敌兵布阵 (zkw线段树)
- 【数字图像处理】四.MFC对话框绘制灰度直方图
- Dancing Links讲解和练习整理
- OpenCV Mat数据类型及位数总结
- C#创建Windows Service(Windows 服务)基础教程
- unable to start the monitor on 4454 ,an other instance is problaly using the same port
- 通过当前IP获取当前网卡的MAC地址