leetcode 28. Implement strStr() 实现strStr()
生活随笔
收集整理的這篇文章主要介紹了
leetcode 28. Implement strStr() 实现strStr()
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
?
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
總結
以上是生活随笔為你收集整理的leetcode 28. Implement strStr() 实现strStr()的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 图解git原理与日常实用指南
- 下一篇: HTML-语义类标签