首页 > 代码库 > 028. Implement strStr()

028. Implement strStr()

 1 class Solution { 2 public: 3     int strStr(string haystack, string needle) { 4         if (needle.size() == 0) return 0; 5         else { 6             if (haystack.size() == 0) return -1; 7             else { 8                 for (int i = 0; i <= static_cast<int>(haystack.size()) - static_cast<int>(needle.size()); ++i) { 9                     bool flag = true;10                     for (int j = 0; j < needle.size(); ++j) {11                         if (haystack[i + j] != needle[j]) {12                             flag = false; break;13                         }14                     }15                     if (flag) {16                         return i;17                     }18                 }19                 return -1;20             }21         }22     }23 };

 

028. Implement strStr()