『垂直遍历』题解 | #最长公共前缀#
最长公共前缀
http://www.nowcoder.com/practice/28eb3175488f4434a4a6207f6f484f47
class Solution { public: /** * * @param strs string字符串vector * @return string字符串 */ string longestCommonPrefix(vector<string>& strs) { // write code here int strsSize=strs.size(); if( 0==strsSize ) { return string();//临时对象 } if( 1==strsSize ) { return strs[0]; } int minLen=0x3f3f3f; for( auto vec : strs ) { if( vec.size()<minLen ) { minLen=vec.si***Len=min( minLen, vec.size() ); } int Len=0; for( int i=0; i<minLen; ++i ) { bool tag=false; char temp=strs[0][i]; for( int loop=1; loop<strsSize; ++loop ) { if( strs[loop][i]!=temp ) { tag=true; break; } } if( tag ) { break; } else { ++Len; } } return strs[0].substr(0,Len); } };