LeetCode : Implement strStr


Implement strStr().

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

在haystack中查找needle第一次出现的位置:

class Solution{
public:
    int strStr(string haystack, string needle) {
        if (needle.length() == 0)
            return 0;
        if (haystack.length() < needle.length())
            return -1;
        for (int i = 0; i<haystack.length(); ++i){
            int j;
            for (j = 0; j<needle.length(); ++j){
                if (haystack[i + j] != needle[j])
                    break;
            }
            if (j == needle.length())
                return i;
        }
        return -1;
    }
};
优质内容筛选与推荐>>
1、jQuery上传文件,含进度条。
2、Ubuntu16.04安装openBLAS
3、POJ2096 Collecting Bugs
4、Python自学第十四周(1)
5、Java集合类框架的最佳实践有哪些


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号