由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - lc 上面4 sum的时间复杂度要求多少?
相关主题
我这个 4sum的解法是 o^3还是o^2? , xiexie请教下3sum为撒超时
find the first missing positive number麻烦大家帮看看这段代码的问题
请教一下leetcode gas station约瑟夫问题 用循环链表算法 时间 复杂度多少
帮俺看一下代码DP+DFS为什么过不了work break II 那个大case : aaaaaaa...问一个reverse int的问题
Surrounded Regions, dfs solution. 过不了online testleetcode 4sum N^3解法有时Time Limit Exceeded有时又能通过
这种backtracking的问题怎么算时间复杂度?比如palindrom patitioning.Leetcode 4SUM 总是超时
Reverse Words in a String 有只扫一遍的inplace的做法吗?求个4sum的算法
【leetcode restore IP address】为什么这种情况一定要用tmp?哪位大侠帮我看看这个code
相关话题的讨论汇总
话题: num话题: start话题: end话题: int话题: vector
进入JobHunting版参与讨论
1 (共1页)
g***j
发帖数: 1275
1
为啥这个可以过? 下面这个时间复杂度是 O^3 吧?
class Solution {
public:
vector > fourSum(vector &num, int target) {
// Note: The Solution object is instantiated only once and is reused
by each test case.
vector tmp;
vector> results;
if(num.empty()) return results;
sort(num.begin(), num.end());
for(int i=0; i {

for(int j=i+1; j {
int temp = target - num[j] - num[i];
int start = j+1, end = num.size()-1;
while(start {
if(num[start]+num[end]==temp)
{
tmp.push_back(num[i]);
tmp.push_back(num[j]);
tmp.push_back(num[start]);
tmp.push_back(num[end]);
results.push_back(tmp);
tmp.clear();
start++;
end--;
while(start while(start }
else if(num[start]+num[end] {
start++;
while(start }
else
{
end--;
while(start }
}
while(j }
while(i }
return results;
}
};
m******3
发帖数: 346
2
应该不是N^3,最里面一层是binary search, 应该复杂度是N^2LogN

reused

【在 g***j 的大作中提到】
: 为啥这个可以过? 下面这个时间复杂度是 O^3 吧?
: class Solution {
: public:
: vector > fourSum(vector &num, int target) {
: // Note: The Solution object is instantiated only once and is reused
: by each test case.
: vector tmp;
: vector> results;
: if(num.empty()) return results;
: sort(num.begin(), num.end());

y*******8
发帖数: 112
g***j
发帖数: 1275
4
are you sure?
里面明明是两个指针

【在 m******3 的大作中提到】
: 应该不是N^3,最里面一层是binary search, 应该复杂度是N^2LogN
:
: reused

1 (共1页)
进入JobHunting版参与讨论
相关主题
哪位大侠帮我看看这个codeSurrounded Regions, dfs solution. 过不了online test
Leetcode Copy List with Random Pointer Runtime Error?这种backtracking的问题怎么算时间复杂度?比如palindrom patitioning.
divide two integersReverse Words in a String 有只扫一遍的inplace的做法吗?
新题gas station,献丑了,没过一个case,帮看看【leetcode restore IP address】为什么这种情况一定要用tmp?
我这个 4sum的解法是 o^3还是o^2? , xiexie请教下3sum为撒超时
find the first missing positive number麻烦大家帮看看这段代码的问题
请教一下leetcode gas station约瑟夫问题 用循环链表算法 时间 复杂度多少
帮俺看一下代码DP+DFS为什么过不了work break II 那个大case : aaaaaaa...问一个reverse int的问题
相关话题的讨论汇总
话题: num话题: start话题: end话题: int话题: vector