leetcode_28_ Implement strStr() (easy)

Imple

ment 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 see your function signature returns a char * or String, please click the reload button to reset your code definition.

解题:

要考虑“”,""情况,输出为0

最初的写法,暴力法:
class Solution {
public:
    int strStr(string haystack, string needle) {
        if(needle.length() == 0) {
            if(haystack.length() == 0)return 0;
            return -1;
        }
        for(int i =0;i<=haystack.length()-needle.length();i++){
            if(haystack.at(i)==needle.at(0)){
                int y = i;
                int equation = 0;
                for(int j = 0;j<needle.length();j++,y++){
                    if(haystack.at(y)!=needle.at(j)){
                        equation = 1;
                        break;
                    }
                }
                if(!equation){
                    return i;
                }
            }
        }
        return -1;
    }
     
};
超时
然后:

这道题的直接思路就是暴力查找,就是一个字符一个字符地进行匹配。不多说了,如果有兴趣,可以去研究这几个O(m+n)的算法:Rabin-Karp算法、KMP算法和Boyer-Moore算法。

时间复杂度:O(mn)

空间复杂度:O(1)

#include <iostream>

using namespace std;class Solution {public:    int strStr(string haystack, string needle) {        if(haystack.length()<needle.length())return -1;//没有此句,会超时        unsigned long al = haystack.length();        unsigned long bl = needle.length();        for(int i =0,j;i<=al-bl;i++){            for( j = 0;j<needle.length()&&haystack.at(i+j)==needle.at(j);j++);                if(j==bl){                    return i;                }        }        return -1;    }    };int main(int argc, const char * argv[]) {    Solution c;    string a;    string b;    while (1) {        cout<<"start"<<endl;        cin>>a;        cin>>b;        cout<<c.strStr(a, b)<<endl;    }      return 0;}
时间: 2024-10-26 15:43:03

leetcode_28_ Implement strStr() (easy)的相关文章

[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】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

28. Implement strStr() [easy] (Python)

题目链接 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. 题目翻译 实现 strStr() 函数.该函数用于判断一个字符串 needle 是否是另一个字符串 haystack 的子串.如果是

leetcode Implement strStr()(easy) /java

我以为,当时我用c++写这个函数的时候,整个人如同乱麻. 这次用java写.先查的SE 8中String的方法.找到两个与此函数有关的方法:matches()和substring(). import java.io.*; import java.util.*; public class Solution { public static int strStr(String haystack, String needle) { int r=-1; int len1=haystack.length()

LeetCode_28. Implement strStr()

28. Implement strStr() Easy 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: ha

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

Leet Code OJ 28. Implement strStr() [Difficulty: Easy]

题目: 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出现的下标,如果needle不是haystack的一部分,就返回-1. 分析: 在文本中查找某个模式出现的位置的算法,称为字符串匹配算法.常用的方法有

leetcode第27题--Implement strStr()

Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack. 就是判断haystack中是否有needle,如果包含的话,返回第一次包含的地址.如果没有则返回NULL. 这题官网打出来的是easy,但是要做好绝不简单.我能想到的就是O(m*n)的复杂度了.最经典的是用KMP算法. class Soluti

LeeCode28 Implement strStr()

题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. (Easy) 分析: 不用考虑KMP啥的,就是写好暴力写法就行. 两重循环,注意空串判定,注意haystack比needle长,注意外层循环的终止条件. 代码: 1 class Solution { 2 public: 3 int strS