LeetCode – Refresh – Implement strStr()

Brute Force:

 1 class Solution {
 2 public:
 3     int strStr(char *haystack, char *needle) {
 4         if (!haystack) return -1;
 5         if (!needle) return 0;
 6         int index = 0, tmp1 = 0, tmp2 = 0;
 7         while (haystack[index]) {
 8             if (haystack[index] == needle[0]) {
 9                char *tmp1 = haystack, *tmp2 = needle;
10                while (tmp1 && tmp2 && *tmp1 == *tmp2) {
11                    tmp1++;
12                    tmp2++;
13                }
14                if (!tmp2) return index;
15             }
16             index++;
17         }
18         return -1;
19     }
20 };

KMP:

Intialize a dp map for search whether there has duplicate chars in needle.

j = dp[i-1] is starting from the previous step

 1 class Solution {
 2 public:
 3     int strStr(char *haystack, char *needle) {
 4         if (!haystack) return -1;
 5         if (!needle) return 0;
 6         int len = strlen(needle), i = 0, j = 0;
 7         vector<int> dp(len, -1);
 8         for (int i = 1; i < len; i++) {
 9             for (j = dp[i-1]; j >= 0 && needle[i] != needle[j+1]; j = dp[j]);
10             if (needle[i] == needle[j+1]) dp[i] = j+1;
11         }
12         i = 0, j = 0;
13         while (haystack[i] && needle[j]) {
14             if (haystack[i] == needle[j]) {
15                 i++;
16                 j++;
17             } else if (j == 0) i++;
18             else j = dp[j-1] + 1;
19         }
20         if (needle[j]) return -1;
21         return i-j;
22     }
23 };

.

时间: 2024-11-06 17:40:51

LeetCode – Refresh – Implement strStr()的相关文章

【LeetCode】Implement strStr()

Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack. 标准KMP算法.可参考下文. http://blog.csdn.net/yaochunnian/article/details/7059486 核心思想在于求出模式串前缀与后缀中重复部分,将重复信息保存在n

[LeetCode] 028. Implement strStr() (Easy) (C++/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 028. Implement strStr() (Easy) 链接: 题目:https://oj.leetcode.com/problems/implement-strstr/ 代码(github):https://github.com/illuz/leetcode 题意: 在一个字符串里找另一个字符串在其中的位置

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】Implement strStr() (easy)

Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 思路: 注意,在for循环中条件有计算得到的负数时, 一定要把计算括起来转换为int, 否则会默认转换为uchar 负数就会被误认为是一个很大的数字. for(int i = 0; i < int(1 - 2); ++i) 实现很常规: int s

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

Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 解法: 这道题让我们在一个字符串中找另一个字符串第一次出现的位置,那我们首先要做一些判断,如果子字符串为空,则返回0,如果子字符串长度大于母字符串长度,则返回-1.然后我们开始遍历母字符串,我们并不需要遍历整个母字符串,而是遍历到剩下的长度和子字符

Java for LeetCode 028 Implement strStr()

Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 解题思路: 直接看代码即可,JAVA实现如下: static public int strStr(String haystack, String needle) { for(int i=0;i<=haystack.length()-needle.l

[LeetCode][Java] 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() 返回needle在haystack中第一次出现的位置,如果haystack中不存在needle,返回-1. 算法分析: 方法一: * in Java, there is an API function name

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