题目
编写一个函数来查找字符串数组中的最长公共前缀。
如果不存在公共前缀,返回空字符串 ""
。
示例
示例1
输入:strs = ["flower","flow","flight"]
输出:"fl"
示例2
输入:strs = ["dog","racecar","car"]
输出:""
解释:输入不存在公共前缀。
提示
- 1 <= strs.length <= 200
- 0 <= strs[i].length <= 200
strs[i]
仅由小写英文字母组成
题解
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
string res;
string& s = strs[0];
for (int i = 0; i < s.size(); i ++ ) {
for (string& str: strs)
if (str.size() == i || str[i] != s[i])
return res;
res += s[i];
}
return res;
}
};
标签:string,示例,strs,res,014,str,LeetCode,前缀
From: https://www.cnblogs.com/yuyork/p/17022854.html