從頭到尾對每個字串比對直到失敗或結束為止即可得解。
C++(4ms)
/*******************************************************/
/* LeetCode 14. Longest Common Prefix */
/* Author: Maplewing [at] knightzone.studio */
/* Version: 2018/10/05 */
/*******************************************************/
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
if(strs.size() == 0) return "";
if(strs.size() == 1) return strs[0];
for(int i = 0 ; i < strs[0].length() ; ++i){
char c = strs[0][i];
for(int j = 1 ; j < strs.size() ; ++j){
if(c != strs[j][i]){
return strs[0].substr(0, i);
}
}
}
return strs[0];
}
};
[…] #LeetCode:14. Longest Common Prefix […]