【28】Implement strStr()

    xiaoxiao2026-05-11  0

    Implement strStr().

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

    直接暴力做就好了 int strStr(string haystack, string needle) { int len1=haystack.length(); int len2=needle.length(); for(int i=0;i<=len1-len2;i++){ bool flag=true; for(int j=0;j<len2;j++){ if(haystack[i+j]!=needle[j]){flag=false;break;} } if(flag)return i; } return -1; }
    转载请注明原文地址: https://ju.6miu.com/read-1309573.html
    最新回复(0)