试题请参见: https://oj.leetcode.com/problems/longest-common-prefix/
题目概述
解题思路
源代码
class Solution {
public:
std::string longestCommonPrefix(std::vector<std::string> &strs) {
bool isMatched = true;
int index = 0;
char character = 0;
if ( strs.size() == 0 ) {
return "";
}
do {
character = strs[0][index];
for ( size_t i = 0; i < strs.size(); ++ i ) {
if ( index >= strs.at(i).size() || strs.at(i).at(index) != character ) {
isMatched = false;
}
}
++ index;
} while ( isMatched );
return strs[0].substr(0, index - 1);
}
};