Implement strStr()
生活随笔
收集整理的這篇文章主要介紹了
Implement strStr()
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
就是在字符串haystack中找到needle 的位置。。。
class Solution { public:int strStr(string haystack, string needle) {int m = haystack.size();int n = needle.size();for(int i = 0; i <= m-n; i ++){int j;for(j = 0; j < n; j ++){if(haystack[i+j] != needle[j])break;}if(j == n)return i;}return -1;} }; #include<string> #include<iostream> #include<algorithm> using namespace std; int strstr(string, string); int main() {string a = "asdfghjklzxcvb";string b = "lzxcvb";cout << strstr(a, b);system("pause");return 0; }int strstr(string a, string b) {int len1 = a.size();int len2 = b.size();if (len1 < len2)return -1;int j;for (int i = 0; i < len1; i++){for (j = 0; j < len2; j++){if (a[i + j] != b[j])break;}if (j == len2)return i;}return -1; }
總結
以上是生活随笔為你收集整理的Implement strStr()的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: blkdiag--生成以输入元素为对角线
- 下一篇: wilkinson--生成Wilkins