首页 > 其他 > 详细

28. Implement strStr()

时间:2017-06-11 22:11:27      阅读:291      评论:0      收藏:0      [点我收藏+]

Implement strStr().

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

非常容易的一道题目,注意各种特殊情况,先列举出corner case,再进行编码。学习参考经典实现。

这种关于字符串操作类的题目在面试时必须能够倒背如流。

int strStr(char* haystack, char* needle) {
    if (haystack == NULL || needle == NULL)
        return -1;
        
    for (int i = 0; ; ++i) {
        for (int j = 0; ; ++j) {
            if (needle[j] == \0)
                return i;
            else {
                if (haystack[i+j] == \0)
                    return -1;
                if (haystack[i+j] != needle[j])
                    break;
            }
        }
    }
}

 

28. Implement strStr()

原文:http://www.cnblogs.com/naivecoder/p/6986434.html

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