leetcode 之Implement strStr()(27)

字符串的匹配,返回匹配开始的位置,直接用暴力方式求解。为了更快的匹配,定义一个指针表示待匹配的字符串的长度,当长度不足时,可

直接停止匹配。

char *strStr(char *haystack, char*needle)
      {
          char* p1;
          char* p2;
          char* p1_advance=haystack;

          //当字符串数量不足时,直接停止匹配
          for (p2 = &needle[1]; *p2; p2++)
              p1_advance++;

          for (p1 = haystack; *p1_advance; p1_advance++)
          {
              char *p1_old = p1;
              p2 = needle;
              while (*p1 && *p2 && *p1 == *p2)
              {
                  p1++;
                  p2++;
              }
              if (!*p2)return p1_old;

              p1 = p1_old + 1;
          }

          return nullptr;
      }

时间: 2024-10-29 11:56:52

leetcode 之Implement strStr()(27)的相关文章

【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

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",

【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