码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode:Implement strStr()

时间:2014-11-18 11:47:35      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:algorithm   leetcode   

题目描述:

Implement strStr().

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

代码:

int Solution::strStr(char *haystack, char *needle)
{
    int haystack_length = strlen(haystack);
    int needle_length = strlen(needle);
    for(int i = 0;i < haystack_length-needle_length+1;i++)
    {
        int index = i;
        int j;
        for(j = 0;j < needle_length;j++,index++)
        {
            if(haystack[index] == needle[j])
                continue;
            else
                break;
        }
        if(j == needle_length)
            return i;
    }
    return -1;
}


LeetCode:Implement strStr()

标签:algorithm   leetcode   

原文地址:http://blog.csdn.net/yao_wust/article/details/41243953

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!