为什么报错啊???错误信息如下:(这个错误的测试案例是[],有内容的能通过)
Line 933: Char 34: runtime error: reference binding to null pointer of type 'struct value_type' (stl_vector.h)
14. Longest Common Prefix
Easy
11421163FavoriteShare
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
.
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
int n=strs.size();
int m=strs[0].size();
string ans="";
int tp=0;
if(strs.empty())return "";
else{
for(int i=1;i<=m;i++)
{
tp=0;
string tpstr=strs[0].substr(0,i);
for(int j=1;j<n;j++)
{
if(strs[j].substr(0,i)==tpstr)tp++;
}
if(tp==(n-1))
{
if(ans.length()<tpstr.length())ans=tpstr;
}
}
return ans;
}
}
};