LeetCode(28)题解:Implement strStr()

https://leetcode.com/problems/implement-strstr/

题目:

Implement strStr().

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

思路:

判断一个string是否另一个string的子序列并返回位置。

naive法:遍历查找,复杂度O(mn)。

advance法还有Rabin-Karp, KMP, Boyer- Moore algorithm等。

AC代码:

 1 class Solution {
 2 public:
 3     int strStr(string haystack, string needle) { 4         int m=haystack.size();
 5         int n=needle.size();
 6         if(n==0 && m==0)
 7             return 0;
 8         bool flag=true;
 9         for(int i=0;i<m-n+1;i++){
10             for(int j=0;j<n;j++){
11                 if(needle[j]!=haystack[i+j]){
12                     flag=false;
13                     break;
14                 }
15             }
16             if(flag==true){
17                 return i;
18             }
19             else
20                 flag=true;
21         }
22         return -1;
23     }
24 };
时间: 2024-11-09 01:39:03

LeetCode(28)题解:Implement strStr()的相关文章

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. 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()

一.题目描述 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()

https://leetcode.com/problems/implement-strstr/ Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 字符串简单题.一定要写的没BUG. 如果不在函数最后一行写return语句的话,LeetCode会出RUNTIME ERROR. Line 27: co

[Leetcode][Python]28: Implement strStr()

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 28: Implement strStr()https://oj.leetcode.com/problems/implement-strstr/ Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of hays

19.1.30 [LeetCode 28] Implement strStr()

Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Example 1: Input: haystack = "hello", needle = "ll" Output: 2 Example 2: Input: haystack = "aaaaa",

44. leetcode 28. Implement strStr()

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. 思路:子串匹配,朴素匹配.

leetCode 28. Implement strStr() 字符串

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. 在haystack中找与needle 第一个相匹配的位置.如果找不到,返回-1. 代码如下: class Solution { public:     int strStr(string haystac

【一天一道LeetCode】#28. Implement strStr()

一天一道LeetCode系列 (一)题目 Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. (二)解题 第一种解法:朴素匹配算法 /* 两个指针,分别指向两个字符串的首字符 如果相等则一起向后移动,如果不同i取第一个相同字符的下一个开始继续匹配 如果最后j等于needle的长度则匹配成功,返回i-