Write a function to find the longest common prefix string amongst an array of strings.
This is easy to understand.
// Longestcommonprefix. cpp: defines the entry point of the console application. // # Include "stdafx. H "# include <vector> # include <string> # include <iostream> using namespace STD; Class solution {public: String longestcommonprefix (vector <string> & STRs) {int Index = 0; If (STRs. size () = 0) {return "";} If (STRs. size () = 1) return STRs [0]; while (true) {bool eq = true; char tempc = STRs [0] [Index]; for (INT I = 1; I <STRs. size (); I ++) {If (index> = STRs [I]. size () {eq = false; break;} If (STRs [I] [ Index]! = Tempc) {eq = false; break;} If (! EQ) break; index ++;} return STRs [0]. substr (0, index) ;}}; int _ tmain (INT argc, _ tchar * argv []) {solution SS; vector <string> res; // res. push_back ("aabbbdkajsk"); Res. push_back ("aabuiuiouiou"); string Fres = ss. longestcommonprefix (RES); cout <Fres <Endl; System ("pause"); Return 0 ;}
[Leetcode] Longest Common prefix