[string]Implement strStr()

Implement strStr().

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

Subscribe to see which companies asked this question

Show Tags

Hide Similar Problems

(H) Shortest Palindrome

1.普通匹配 o(n*m)

class Solution {
public:
    int strStr(string haystack, string needle) {
        int haystackSize = haystack.size();
        int needleSize = needle.size();
        if(needleSize ==0){
            return 0;
        }
        int resIndex = 0;
        int i=0,j=0,k=0;
        while(i<haystackSize && j<needleSize){
            if(k>=haystackSize){
                break;
            }
            if(haystack[k]==needle[j]){
                k++;
                j++;
            }else{
                i++;
                k = i;
                j = 0;
            }
        }
        resIndex = j==needleSize ? i:-1;
        return resIndex ;
    }
};

2.KMP o(n)

时间: 2024-09-30 15:35:08

[string]Implement strStr()的相关文章

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

Implement strStr()

package cn.edu.xidian.sselab.string; /** *  * @author zhiyong wang * title: Implement strStr() * content: *  Implement strStr(). *   *  Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.  * */pub

【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

LeetCode 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

Implement strStr()——字符串中第一次出现给定字符串的位置

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41452047 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 func

【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笔记: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

【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

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