leetcode028:Implement strStr()

问题描述

Implement strStr().

Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

Update (2014-11-02):

The signature of the function had been updated to return the index instead of the pointer. If you still see your function signature returns a char * or String, please click the reload button  to reset your code definition.

分析

查找字符串needle在haystack第一次出现的位置索引。这里需要用到已匹配部分的历史信息,从而减少字符比较次数。举例:haystack:abcdabXXX,needle:abcdabXXX,这里我们可以利用的历史信息即为ab,这样下次开始比较时从ab开始匹配。

代码

这里m用来记录失败匹配时已匹配部分有多少无需再比较的部分的字符数,上述例子m=2.

//运行时间:8ms
class Solution {
public:
	int strStr(string haystack, string needle) {
		if (needle.empty()) return 0;
		int h_len = haystack.length();
		int n_len = needle.length();
		if (h_len < n_len) return -1;
		//int ans = haystack.find(needle, 0);
		//if (ans == string::npos) return -1;
		//return ans;
		int i = 0;
		int m = 0;
		int j = 0;
		bool flag = false;
		int ans;
		while (i < h_len){
			ans = i - m;
			j = m;
			m = 0;
			while (i < h_len&&j < n_len&&haystack[i] == needle[j]){
				if (j){
					if (needle[j] == needle[m])
						m++;
					else m = 0;
				}
				i++; j++;
			}
			if (j == n_len) { flag = true; break; }
			if (!j) i++;
		}
		if (flag) return ans;
		return -1;
	}
};
时间: 2024-10-26 22:09:29

leetcode028:Implement strStr()的相关文章

leetcode笔记:Implement strStr()

一.题目描述 Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack. 二.题目分析 实现strstr()函数.返回needle(关键字)在haystack(字符串)中第一次出现的位置,如果needle不在haystack中,则返回-1.由于使用暴力方法的时间复杂度为O(mn)会超时,可使用著名的KM

LeetCode 28:Implement strStr()

Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 实现函数strStr.代码如下: int strStr(char* haystack, char* needle) { size_t len = strlen(haystack); if(strlen(needle) == 0) return 0;

【LeetCode】28. Implement strStr() 解题小结

题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 简单题. class Solution { public: int strStr(string haystack, string needle) { if (needle.empty()) return 0; if (haystack.em

【Implement strStr() 】cpp

题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Update (2014-11-02):The signature of the function had been updated to return the index instead of the pointer. If you st

[Leetcode] implement strStr() (C++)

题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Update (2014-11-02):The signature of the function had been updated to return the index instead of the pointer. If you st

LeeCode28 Implement strStr()

题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. (Easy) 分析: 不用考虑KMP啥的,就是写好暴力写法就行. 两重循环,注意空串判定,注意haystack比needle长,注意外层循环的终止条件. 代码: 1 class Solution { 2 public: 3 int strS

leetcode_171 Excel Sheet Column Number &amp; leetcode_28 Implement strStr()

leetcode_171 Excel Sheet Column Number 题目: Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, return its corresponding column number. 解法: class Solution { public: int titleToNumber(string s) { int num = 0;

Java [leetcode 28]Implement strStr()

题目描述: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Update (2014-11-02): The signature of the function had been updated to return the index instead of the pointer. If you

LeetCode 28 Implement strStr() 找到字串返回位置。

题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 翻译: 找到字符串的子串位置,并返回.如果没有则返回-1 思路: 通过最简单的BF遍历,如果不符合则指向下一个字符,最后如果长度等于子串长度,则返回位置. 代码: public static int strStr(String haysta