OJ练习13——T28 Implement strStr()

实现字符串strstr()的功能,即从母串中查找子串第一次出现的位置(序号),没有出现则返回-1.

【思路】

刚开始学c时就学到过这个例子。

不同之处在于,这里参数是string,处理起来要比char*简单。

【my code】

int strStr(string haystack, string needle) {
    if(needle=="")
        return 0;
    if(haystack.size()<needle.size())
        return -1;
    int i=0, j=0, k;
    for(i=0; i<=haystack.size()-needle.size(); i++)
    {
        k=i;
        j=0;
        while(j<needle.size())
        {
            if(haystack[k]==needle[j])
            {
                k++;j++;
            }
            else
                break;
        }
        if(j==needle.size()){
            return i;
        }
    }
    return -1;
}

【出现的问题】

1.输入("", "")和("abcd", ""),("a", "aaaa")的特殊情况都要考虑;

2.易错!——最外层for循环,i的范围应该在i<=haystack.size()-needle.size(),否则会发生越界,报错为:

time limit exceed(like this?)

一定要注意数组越界访问的问题!

【番外】

看到网上有用KMP实现的,顺便复习一下。

时间: 2024-11-05 20:26:58

OJ练习13——T28 Implement strStr()的相关文章

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

勒etcode 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. Update (2014-11-02):The signature of the function had been updated to return the index instead of the pointer. If you still

[Leetcode] implement strStr() (C++)

题目: 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

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

2016.6.18——Implement strStr()

Implement strStr() 本题收获: 1.考虑多种边界条件. 2.haystack.size()和 int n = haystack.size()的区别(现在还不知道) 题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 思路: 注意题目中并没有说haystack.size()

[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

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

Implement strStr() leetcode java

题目: 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的子串,是的话就返回这个子串. 解题想法是,从haystack的第