【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.

提示:

此题可以使用暴力搜索方法。当然也可以使用其他一些高级方法,如:Rabin-Karp, KMP等等。这些算法打算等之后对他们更了解了以后,单独写一些文章对其总结。因此这里仅给出暴力搜索方法。

代码:

class Solution {
public:
    int strStr(string haystack, string needle) {
    int hay_len = haystack.size();
    int needle_len = needle.size();
    if (needle_len == 0) return 0;
    if (hay_len == 0) return -1;
    int i = 0, j = 0, p;
    for (i = 0; i <= hay_len - needle_len; ++i) {
        for (p = i, j = 0; j < needle_len; ++j, ++p) {
            if (haystack[p] != needle[j]) break;
        }
        if (j == needle_len) return i;
    }
    return -1;
}
};
时间: 2024-10-08 22:23:24

【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. 简单题. class Solution { public: int strStr(string haystack, string needle) { if (needle.empty()) return 0; if (haystack.em

【一天一道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-

[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

【LeetCode】225 - Implement Stack using Queues

Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. empty() -- Return whether the stack is empty. Notes: You must use only s

【LeetCode】232. Implement Queue using Stacks

Implement Queue using Stacks Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from in front of queue. peek() -- Get the front element. empty() -- Return whether t

【LeetCode】28.Linked List— Remove Linked List Elements删除链表元素

Remove all elements from a linked list of integers that have value val. Example: Input: 1->2->6->3->4->5->6, val = 6 Output: 1->2->3->4->5 说到删除,首先想到定义两个指针,分别指向要被删除的结点和该结点的前驱结点.这里还需要考虑头结点是需要删除结点的特殊情况. /** * Definition for sing

28. Implement strStr()-leetcode-java

[原来在SAE的blog上,都转到CSDN了..] 28. Implement strStr()-leetcode-java 发表于 2016/02/06 题意 Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 字符串匹配问题,如果子串在主串中存在,则返回子串在主串中首次出现的第一个字符的位置,不

【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

28. Implement strStr()【easy】

28. 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. 解法一: 1 class Solution { 2 public: 3 int strStr(string haystack, string needle) { 4 if (haystack