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