由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 大家帮忙分析下leetcode一个题目的复杂度
相关主题
[合集] G家onsite面经Leetcode Word Break I 有o(n^2)的算法吗?
leetcode-- scramble stringPalindrome Partitioning II 的DP做法?
有人面试碰到过scramble string这个题吗?Docode 问题
L家 Influencer 问题求讨论 微软面世经过
请教n queen 问题的time complexity贴几道某大公司的面试题
问一个面试问题facebook电面题目
Find consecutive repeated string问一个老的google面试题
wildcard matching 大case runtime errorAsk a google interview question(3)
相关话题的讨论汇总
话题: strs话题: return话题: isequal话题: index话题: start
进入JobHunting版参与讨论
1 (共1页)
a********e
发帖数: 53
1
class Solution {
public:
Write a function to find the longest common prefix string amongst an array
of strings.
The Time complexity is O(logM * N), while M is the size of strs, N is the
shortest length of the str in the strs
而我觉得这个时间复杂度应该是O(MN). 因为isEqual()的复杂度应该是O(M), as T(M
) = 2T(M/2) + O(1). 谢谢。
============================
bool isEqual(vector &strs, int index, int start, int end) {
if(end < start) return true;
if(end == start) {
if(strs[start].size() > index) return true;
else return false;
}
int mid = (start + end) / 2;
bool lEqual = isEqual(strs, index, start, mid);
bool rEqual = isEqual(strs, index, mid + 1, end);
if(!lEqual || !rEqual) return false;
if(index > strs[start].size()) return false;
if(index > strs[end].size()) return false;
if(strs[start][index] != strs[end][index]) return false;
return true;
}
string longestCommonPrefix(vector &strs) {
// Note: The Solution object is instantiated only once and is reused by
each test case.
string res;
size_t vSize = strs.size();
if(0 == vSize) return res;
if(1 == vSize) return strs[0];
int len = 0;
while(isEqual(strs, len, 0, vSize - 1)) {
len ++;
}
res = strs[0].substr(0, len);
return res;
}
1 (共1页)
进入JobHunting版参与讨论
相关主题
Ask a google interview question(3)请教n queen 问题的time complexity
L二电面据,附面经问一个面试问题
写一个function判断一个数是不是2的整数次方Find consecutive repeated string
leetcode wordsearch的时间复杂度?wildcard matching 大case runtime error
[合集] G家onsite面经Leetcode Word Break I 有o(n^2)的算法吗?
leetcode-- scramble stringPalindrome Partitioning II 的DP做法?
有人面试碰到过scramble string这个题吗?Docode 问题
L家 Influencer 问题求讨论 微软面世经过
相关话题的讨论汇总
话题: strs话题: return话题: isequal话题: index话题: start