首页 > 其他 > 详细

LeetCode 28:Implement strStr()

时间:2015-06-04 09:51:59      阅读:224      评论:0      收藏:0      [点我收藏+]

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

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

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