和大神们学习每天一题(leetcode)-Implement strStr()
来源:互联网 发布:ubuntu下wine中安装qq 编辑:程序博客网 时间:2024/04/27 19:49
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
题意是在一个字符串中查找目标字符串的第一个位置,如果没有返回-1。
这道题虽然题意很简单,但是细节较多,需要考虑目标字符串为空的情况还有当在某个位置匹配字符串失败后,重新比较开始的位置。
class Solution {public:<span style="white-space:pre"></span>int strStr(char *haystack, char *needle) <span style="white-space:pre"></span>{<span style="white-space:pre"></span>if (*needle == '\0')//如果目标字符串为空则返回0<span style="white-space:pre"></span>return 0;<span style="white-space:pre"></span>char *ChpSta = haystack, *ChpNee = needle;<span style="white-space:pre"></span>int nPos = -1,nMove = 0;//定义两个标记分别为比较开始的位置和当前比较的位置<span style="white-space:pre"></span>while (*ChpSta != '\0')//遍历查找字符串<span style="white-space:pre"></span>{<span style="white-space:pre"></span>if (*ChpNee == *ChpSta)//比较字符串<span style="white-space:pre"></span>{<span style="white-space:pre"></span>*ChpNee++;<span style="white-space:pre"></span>if (nPos == -1)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>nPos = nMove;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}<span style="white-space:pre"></span>else<span style="white-space:pre"></span>{<span style="white-space:pre"></span>if (nPos != -1)//匹配失败时将当前比较的位置重新定义到前一次比较开始的位置的下一位<span style="white-space:pre"></span>{<span style="white-space:pre"></span>ChpSta = ChpSta - (nMove - nPos);<span style="white-space:pre"></span>nMove = nPos;<span style="white-space:pre"></span>nPos = -1;<span style="white-space:pre"></span>ChpNee = needle;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}<span style="white-space:pre"></span>nMove++;<span style="white-space:pre"></span>ChpSta++;<span style="white-space:pre"></span>if (*ChpNee == '\0')//如果匹配完则返回比较开始位置<span style="white-space:pre"></span>{<span style="white-space:pre"></span>return nPos;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}<span style="white-space:pre"></span>return -1;//没有找到则返回-1<span style="white-space:pre"></span>}};
0 0
- 和大神们学习每天一题(leetcode)-Implement strStr()
- 和大神们学习每天一题(leetcode)-Symmetric Tree
- 和大神们学习每天一题(leetcode)-Same Tree
- 和大神们学习每天一题(leetcode)-Add Binary
- 和大神们学习每天一题(leetcode)-Reverse Integer
- 和大神们学习每天一题(leetcode)-Sort Colors
- 和大神们学习每天一题(leetcode)-Palindrome Number
- 和大神们学习每天一题(leetcode)-ZigZag Conversion
- 和大神们学习每天一题(leetcode)-Valid Palindrome
- 和大神们学习每天一题(leetcode)-Path Sum
- 和大神们学习每天一题(leetcode)-Valid Parentheses
- 和大神们学习每天一题(leetcode)-Remove Element
- 和大神们学习每天一题(leetcode)-Climbing Stairs
- 和大神们学习每天一题(leetcode)-Plus One
- 和大神们学习每天一题(leetcode)-Valid Sudoku
- 和大神们学习每天一题(leetcode)-Two Sum
- 和大神们学习每天一题(leetcode)-3Sum
- 和大神们学习每天一题(leetcode)-4Sum
- Servlet之Listener监听器
- Linux 系统内核的调试
- 广州火车站/东站接送客攻略 停车需谨慎
- Java:String和Date、Timestamp之间的转换
- Spring定时实际项目运用
- 和大神们学习每天一题(leetcode)-Implement strStr()
- 如何通过 JavaScript 在运行时操作 CSS
- DWZ框架搭建 以及使用
- jfinal框架教程-学习笔记(二)
- 关于 content中UA设置,导致网页onPageStarted会重新调用
- iOS发送Email的两种方法
- 排序算法总结---交换排序之起泡排序
- 凸优化基础
- 关于eclipse 3.5.0安装Git插件出错解决方法