题目:
Write a function to find the longest common prefix string amongst an array of strings.
If there is no common prefix, return an empty string ""
.
Example 1:
Input: ["flower","flow","flight"] Output: "fl"
Example 2:
Input: ["dog","racecar","car"] Output: "" Explanation: There is no common prefix among the input strings.
Note:
All given inputs are in lowercase letters a-z
.
题意:
求字符串数组最长公共前缀
思路:
暴力刚
Code:
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
string s="";
int i,j,k=0,n=strs.size();
if(!n) return s;
if(n==1) return strs[0];
string t=strs[0];
int m=t.length();
for(i=0;i<m;i++){
for(j=1;j<n;j++){
if(strs[j].length()==i||strs[j][i]!=t[i]){
k=1;
break;
}
}
if(k) break;
s+=t[i];
}
return s;
}
};