标签:
试题请參见: 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);
}
};LeetCodeOJ. Longest Common Prefix
标签:
原文地址:http://www.cnblogs.com/hrhguanli/p/4505819.html