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

LeetCode 28:Implement strStr()

时间:2015-06-04 09:53:05      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:leetcode   strstr   

Implement strStr().

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

实现函数strStr。代码如下:

int strStr(char* haystack, char* needle) {
    size_t len = strlen(haystack);
    if(strlen(needle) == 0)
    return 0;
    if(strcmp(haystack, needle) == 0)
        return 0;
    for(int i=0; i<len;i++){
        if(haystack[i] == needle[0]){
            if(strncmp(&(haystack[i]), needle, strlen(needle)) == 0)
                return i;
        }
    }
    return -1;
    
}




LeetCode 28:Implement strStr()

标签:leetcode   strstr   

原文地址:http://blog.csdn.net/sunao2002002/article/details/46352927

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