[LeedCode OJ]#28 Implement strStr()

【 声明:版权全部,转载请标明出处,请勿用于商业用途。  联系信箱:[email protected]】

题目链接:https://leetcode.com/problems/implement-strstr/

题意:

给定两个串,判定needle串是否haystack串的子串,假设是,返回匹配的起始位置。假设不是,则返回-1

思路:

直接两个循环暴力解决

class Solution
{
public:
    int strStr(string haystack, string needle)
    {
        int len1 = haystack.length(),len2 = needle.length();
        int i,j;
        if(len2>len1) return -1;
        if(len1 == len2 && haystack!=needle) return -1;
        for(i = 0; i<=len1-len2; i++)
        {
            for(j = 0; j<len2; j++)
            {
                if(haystack[i+j]!=needle[j])
                    break;
            }
            if(j == len2)
                return i;
        }
        return -1;
    }
};
时间: 2024-10-06 16:11:29

[LeedCode OJ]#28 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][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

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() 字符串

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

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

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. 字符串匹配问题,如果子串在主串中存在,则返回子串在主串中首次出现的第一个字符的位置,不

28. Implement strStr()(js)

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

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