2016.6.18--implement StrStr ()

Source: Internet
Author: User

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 ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.