Leetcode-Implement strStr()

来源:互联网 发布:查询linux ipv6 arp 编辑:程序博客网 时间:2024/06/14 22:40
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 len1 = haystack.length();int len2 = needle.length();if (!len2)return 0;for (int i = 0; i < len1 - len2 + 1; i++) {int j = 0;for (; j < len2; ++j) {if (haystack[i + j] != needle[j])break;if (j == len2)return i;}return -1;}}};