14Longest Common Prefix

Source: Internet
Author: User

The article directory is as follows

(1) Your own ideas

(2) your own code

(3) Other people's ideas

(4) Someone else's code

(5) Compare your shortcomings

The topics are as follows:

Write a function to find the longest common prefix string amongst an array of strings.

(1) Your own ideas

A. First, select all the longest prefixes in the string to compare the characters of the same columns in each string vertically, and if they are equal, go ahead and if they find unequal, return the previously scanned string.

B. The length of these prefixes is no longer than the length of the shortest string in all strings.

(2) your own code

classSolution { Public:    stringLongestcommonprefix (vector<string>&STRs) {        intMinlen =Int_max; intStrslen =strs.size ();  for(inti =0; i < Strslen; i++)        {            if(Minlen >strs.at (i). Length ()) Minlen=strs.at (i). Length (); } Vector<Char>pref; intIsfinish =false; CharTMPC =' /'; CharTMPC =' /';  for(intm =0; m < Minlen; m++) {TMPC= strs.at (0) [M];  for(intn =0; n < Strslen; n++) {TMPC=strs.at (n) [m]; if(TMPC! =TMPC) {Isfinish=true;  Break; }            }            if(isfinish) Break;        Pref.push_back (TMPC); }        intPrelen =pref.size (); stringPrefix (Prelen,' /');  for(intt =0; t < Prelen; t++) {prefix.replace (T,1,1, pref.at (t)); }        returnprefix; }};

(3) Other people's ideas

The idea is the same, but other people's code is really concise, and clear!

(4) Someone else's code

classSolution { Public:    stringLongestcommonprefix (vector<string> &STRs) {        if(Strs.empty ())return "";  for(intIDX =0; IDX < strs[0].size (); ++IDX) {//longitudinal scan             for(inti =1; I < strs.size (); ++i) {if(Strs[i][idx]! = strs[0][IDX])returnstrs[0].SUBSTR (0, IDX); }        }        returnstrs[0]; }};

(5) Compare your shortcomings

A. If you write the code is writing a composition, I always feel that I use the vernacular, others use classical Chinese ...

B. My code can be compiled and run on VS2015 (the result is correct), but there is an error running on the website, and I don't know why ...

14Longest Common Prefix

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.