LeetCode 14: Longest Common Prefix
2013-08-26 14:42
495 查看
Difficulty: 2
Frequency: 1
Problem:
Write a function to find the longest common prefix string amongst an array of strings.
Solution:
class Solution {
public:
string longestCommonPrefix(vector<string> &strs) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (strs.size()==0)
{
return string("");
}
return longestCommonPrefixBinaryDivide(strs, 0, strs.size()-1);
}
string longestCommonPrefixBinaryDivide(vector<string> & strs, int i_begin, int i_end)
{
if (i_begin==i_end)
return strs[i_begin];
if (i_begin==(i_end - 1))
{
int i = 0;
while (i<strs[i_begin].size()&&i<strs[i_end].size()&&strs[i_begin][i]==strs[i_end][i])
{
++i;
}
return string(strs[i_begin].substr(0, i));
}
string str1(longestCommonPrefixBinaryDivide(strs, i_begin, (i_begin+i_end)/2));
string str2(longestCommonPrefixBinaryDivide(strs, (i_begin+i_end)/2+1, i_end));
int i = 0;
while (i<str1.size()&&i<str2.size()&&str1[i]==str2[i])
{
++i;
}
// string answer(str1.substr(0, i));
return string(str1.substr(0, i));
}
};
Note:
It is like a binary search. Time complexity is O(mlogn). n is the number of strings, m is the strings' length.
Don't know if there is better answer. If you know please tell me.
Frequency: 1
Problem:
Write a function to find the longest common prefix string amongst an array of strings.
Solution:
class Solution {
public:
string longestCommonPrefix(vector<string> &strs) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (strs.size()==0)
{
return string("");
}
return longestCommonPrefixBinaryDivide(strs, 0, strs.size()-1);
}
string longestCommonPrefixBinaryDivide(vector<string> & strs, int i_begin, int i_end)
{
if (i_begin==i_end)
return strs[i_begin];
if (i_begin==(i_end - 1))
{
int i = 0;
while (i<strs[i_begin].size()&&i<strs[i_end].size()&&strs[i_begin][i]==strs[i_end][i])
{
++i;
}
return string(strs[i_begin].substr(0, i));
}
string str1(longestCommonPrefixBinaryDivide(strs, i_begin, (i_begin+i_end)/2));
string str2(longestCommonPrefixBinaryDivide(strs, (i_begin+i_end)/2+1, i_end));
int i = 0;
while (i<str1.size()&&i<str2.size()&&str1[i]==str2[i])
{
++i;
}
// string answer(str1.substr(0, i));
return string(str1.substr(0, i));
}
};
Note:
It is like a binary search. Time complexity is O(mlogn). n is the number of strings, m is the strings' length.
Don't know if there is better answer. If you know please tell me.
相关文章推荐
- leetcode——14——Longest Common Prefix最长公共前缀
- leetcode[14]Longest Common Prefix
- 【leetcode】14. Longest Common Prefix
- 【leetcode】14. longest common prefix
- [LeetCode] 14. Longest Common Prefix
- Leetcode 14. Longest Common Prefix
- Leetcode 14 Longest Common Prefix
- leetcode[14]:Longest Common Prefix
- [leetcode 14] Longest Common Prefix
- leetcode_14_Longest Common Prefix (easy)
- [Leetcode]14. Longest Common Prefix
- [Leetcode]#14 Longest Common Prefix
- LeetCode 14_Longest Common Prefix
- leetcode题解-13. Roman to Integer && 14. Longest Common Prefix && 20. Valid Parentheses
- Leetcode-14: Longest Common Prefix
- LeetCode_OJ【14】Longest Common Prefix
- [LeetCode-Algorithms-14] "Longest Common Prefix" (2017.10.2-WEEK5)
- 【leetcode】【14】Longest Common Prefix
- LeetCode 14 Longest Common Prefix(最长公共前缀)
- leetcode 14 Longest Common Prefix