1.描述
Write a function to find the longest common prefix string amongst an array of strings.
2.分析
3.代码
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
if (strs.size() <= 0) return "";
unsigned int right = strs[0].size();
for (unsigned int i = 1; i < strs.size(); ++i) {
for (unsigned int j = 0; j < right; ++j) {
if (strs[0][j] != strs[i][j]) {
right = j;
break;
}
}
}
return strs[0].substr(0,right);
}
};
网友评论