Leetcode 28. Implement strStr() (Easy) (cpp)

来源:互联网 发布:禁入证券市场 知乎 编辑:程序博客网 时间:2024/05/23 14:44

Leetcode 28. Implement strStr() (Easy) (cpp)

Tag: Two Pointers, String

Difficulty: Easy


/*28. Implement strStr() (Easy)Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.*/class Solution {public:int strStr(string haystack, string needle) {int haylen = haystack.length(), neelen = needle.length();for (int i = 0, j; i <= haylen - neelen; i++) {for (j = 0; j < neelen && haystack[i + j] == needle[j]; j++) {            }            if (j == neelen) return i;        }        return -1;            }};


0 0