标签:
https://leetcode.com/problems/implement-strstr/
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Line 27: control reaches end of non-void function [-Werror=return-type]
1 #include <iostream> 2 #include <string> 3 using namespace std; 4 5 class Solution { 6 public: 7 int strStr(string haystack, string needle) 8 { 9 // Corner case 10 if (needle.empty()) return 0; 11 if (haystack.empty()) return -1; 12 13 int n = haystack.length() - needle.length() + 1; 14 15 // better use < rather than <= 16 for (int i = 0; i < n; i ++) 17 { 18 bool flag = true; 19 20 for (int j = 0; j < needle.length(); j ++) 21 { 22 if (haystack.at(i + j) != needle.at(j)) 23 { 24 flag = false; 25 break; 26 } 27 } 28 29 if (flag) 30 return i; 31 } 32 33 return -1; 34 } 35 }; 36 37 int main () 38 { 39 Solution testSolution; 40 string s1 = "mississippi"; 41 string s2 = "issi"; 42 43 cout << testSolution.strStr(s1, s2) << endl; 44 45 getchar(); 46 47 return 0; 48 }
LeetCode 28. Implement strStr()
标签:
原文地址:http://www.cnblogs.com/pegasus923/p/5615555.html