C++代码,题目相对不是很难
1 class Solution { 2 public: 3 int strStr(string haystack, string needle) { 4 if(needle.empty()) return 0; 5 int m=haystack.size(),n=needle.size(); 6 int flag=-1; 7 for(int i=0;i<=m-n;i++){ 8 if(haystack[i]==needle[0]){ 9 flag=i; 10 for(int j=1;j<n;j++){ 11 if(haystack[i+j]!=needle[j]){ 12 flag=-1;break; 13 } 14 } 15 if(flag>-1) return flag; 16 } 17 } 18 return flag; 19 } 20 };
原文地址:https://www.cnblogs.com/joelwang/p/10434805.html
时间: 2024-10-30 13:19:51