首页 > 其他 > 详细

[LeedCode OJ]#28 Implement strStr()

时间:2017-07-16 18:49:57      阅读:237      评论:0      收藏:0      [点我收藏+]

【 声明:版权全部,转载请标明出处,请勿用于商业用途。  联系信箱:libin493073668@sina.com】


题目链接: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;
    }
};


[LeedCode OJ]#28 Implement strStr()

原文:http://www.cnblogs.com/lytwajue/p/7191214.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!