Easy 9 Implement strStr()(28)

来源:互联网 发布:纸质书情怀 知乎 编辑:程序博客网 时间:2024/06/05 12:49

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

Solution
暴力法。

class Solution {public:    int strStr(string haystack, string needle) {     if(haystack.empty()&&needle.empty()) return  0;      int m=haystack.size(),n=needle.size();     for(int i=0;i<=m-n;i++){         int j=0;         for(;j<n;j++){             if(needle[j]!=haystack[i+j]) break;         }         if(j==n) return i;     }     return -1;    }};
0 0
原创粉丝点击