Implement StrStr ()
Harvest:
1. Consider a variety of boundary conditions.
The difference between 2.haystack.size () and int n = haystack.size () (not known now)
Topic:
Implement strStr ().
Returns the index of the first occurrence of needle in haystack, or -1 if needleare not part of haystack.
Ideas:
Note that the title does not say Haystack.size () must be greater than needle.size ()
My idea: Brute force search, using two for loops, contrasted one by one without considering some boundary conditions.
Leetcode: Violent search, but considering full boundary conditions
Boundary conditions to consider: haystack, needle
1. "", "" "Output 0
2. "", "a" output-1
3. "A", "" "Output 0
4. "Mississippi", "Issip" Output 4
5. "AA", "AAAA" output-1
6. "A", "a" output 0, note normal, starting from 0 or 1 (the first position is 0 or 1)
Code: The correct code
1 classSolution {2 Public: 3 intSTRSTR (stringHaystackstringneedle) {4 intm = Haystack.length (), n =needle.length ();5 if(!n)return 0;6 for(inti =0; I < M-n +1; i++) {7 intj =0;8 for(; J < N; j + +)9 if(Haystack[i + j]! =Needle[j])Ten Break; One if(j = = N)returni; A } - return-1; - } the};
Special case: Haystack:mississippi Needle:issip
My Code:
1. Problems with ideas
1 classSolution {2 Public:3 intSTRSTR (stringHaystackstringneedle) {4 if(haystack.size () = =0&& needle.size () = =0)return 0;5 if(Haystack.size ()! =0&& needle.size () = =0)return 0;6 if(haystack.size () = =0&& needle.size ()! =0)return-1;7 intn =0;8 intj =needle.size ();9 for(size_t i =0; I < haystack.size (); i++)Ten { One for(size_t j =0; J < Needle.size (); J + +) A { - if(Needle[j]! =Haystack[i]) - { thej =0; -n++; - Break; - } + } - returnN-needle.size () +1; + } A at } -};
Many boundary conditions are not met, each test plus an if statement, code redundancy is more and more high, if the addition of two if statement is also a mistake to consider whether it is the problem of thinking.
Test full code:
There is a problem: I do not need m = Haystack.size (), and n = needle.size (), it is necessary to add the IF statement to determine the size of haystack.size (), Needle.size (), but after the m,n will not need to add if statement, Why????
1 //Implement strStr (). CPP: Defines the entry point of the console application. 2 //consider multi-point test conditions3 //4 5#include"stdafx.h"6#include"iostream"7#include"Stack"8 using namespacestd;9 Ten classMyClass One { A Public: - intSTRSTR (stringHaystackstringNeedle//where does the brother's code imply haystack.size () >= needle.size ()???? - { the if(needle.size () = =0)return 0; - intm = Haystack.size (), n = needle.size ();//Why did you change it? - //if (haystack.size () >= needle.size ()) - //{ + for(inti =0; I < m-n+1; i++) - { + intj =0; A for(; J < Needle.size (); j + +) at { - if(Haystack[i + j]! =Needle[j]) - Break; - } - if(J = =needle.size ()) - { in returni +1; - } to } + //} - return-1; the } * }; $ Panax Notoginseng /* - class MyClass { the Public : + int StrStr (String haystack, string needle) { A int m = haystack.length (), n = needle.length (); the if (!n) return 0; + for (int i = 0; i < m-n + 1; i++) { - int j = 0; $ For (; J < N; j + +) $ if (haystack[i + j]! = Needle[j]) - Break ; - if (j = = n) return i; the } - return-1;Wuyi } the };*/ - Wu - int_tmain (intARGC, _tchar*argv[]) About { $ stringHay ="ABB"; - stringNeed ="abaaa"; - MyClass solution; - intm =0; Am =solution.strstr (hay, need); +cout << M <<Endl; theSystem"Pause"); - return 0; $}
2016.6.18--implement StrStr ()