题目链接

【题解】

两重循环枚举[i..j]这个区间
同时规定必取nums[i]和nums[j]
那么现在的问题就变成在下标为[i..j]这个区间的数字里面找两个数字使他们的和为target-nums[i]-nums[j].
这个问题可以在O(N)的复杂度解决。
所以复杂度就是$O(N^3)$
当然也可以用meet-in-middle写成O(N^2)的
在map里面存个和为x的两个数分别有什么(存他们俩的下标对)。
然后再次两重循环去找map中和target-nums[i]-nums[j]的pair有多少。
然后合并。
去掉有相同下标的情况(一个数字用了两次);

【代码】

class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
vector<vector<int> > ans;
ans.clear();
vector<int> temp;
temp.resize(4);
sort(nums.begin(),nums.end());
int len = nums.size();
for (int i = 0;i < len;i++){
if (i>0 && nums[i]==nums[i-1]) continue;
for (int j = i+3;j<len;j++){
while(j+1<len && nums[j+1]==nums[j]) j++;
//在i..j这个范围内找和为target-nums[i]-nums[j]的数字
int key = target-nums[i]-nums[j];
int l = i+1,r = j-1;
while (l<r){
if (nums[l]+nums[r]>key){
r--;
}else if (nums[l]+nums[r]<key){
l++;
}else {
temp[0] = nums[i];temp[1] = nums[l];
temp[2] = nums[r];temp[3] = nums[j];
ans.push_back(temp);
while (l+1<r && nums[l+1]==nums[l]) l++;
while (r-1>l && nums[r-1]==nums[r]) r--;
l++;r--;
}
}
}
}
return ans;
}
};

【LeetCode 18】四数之和的更多相关文章

  1. Java实现 LeetCode 18 四数之和

    18. 四数之和 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target ...

  2. LeetCode 18. 四数之和(4Sum)

    题目描述 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等? ...

  3. [Leetcode 18]四数之和 4 Sum

    [题目] Given an array nums of n integers and an integer target, are there elements a, b, c, and d in n ...

  4. [LeetCode] 18. 四数之和

    题目链接:https://leetcode-cn.com/problems/4sum/ 题目描述: 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个 ...

  5. LeetCode:四数之和【18】

    LeetCode:四数之和[18] 题目描述 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c ...

  6. 【LeetCode】18.四数之和

    题目描述 18. 四数之和 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 t ...

  7. 代码随想录第七天| 454.四数相加II、383. 赎金信 、15. 三数之和 、18. 四数之和

    第一题454.四数相加II 给你四个整数数组 nums1.nums2.nums3 和 nums4 ,数组长度都是 n ,请你计算有多少个元组 (i, j, k, l) 能满足: 0 <= i, ...

  8. 【LeetCode】四数之和

    [问题]给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找 ...

  9. [LeetCode] 4Sum 四数之和

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...

  10. 【LeetCode】四数之和【排序,固定k1,k2,二分寻找k3和k4】

    给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满 ...

随机推荐

  1. 使用springBoot完成阿里云短信验证

    <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot ...

  2. java开源项目

    原文地址:http://blog.longjiazuo.com/archives/2625 1.整理出一些使用比较广或者个人觉得比较好的java开源项目和资料供参考.2.如果你觉得好但是我没有列出的开 ...

  3. idea创建ssm框架步骤

    打开idea 编辑器 File>new >project 选择Maven 右边勾选Create from archctype   然后下拉选择org.apache.maven.archet ...

  4. bzoj 2015

    http://www.lydsy.com/JudgeOnline/problem.php?id=2015 裸最短路(' '     ) 不过我最初以为是mst (' '    ) #include & ...

  5. CDN技术之--集群服务与负载均衡

    Web集群是由多个同时运行同一个web应用的服务器组成,在外界看来就像一个服务器一样,这多台服务器共同来为客户提供更高性能的服务.集群更标准的定义是:一组相互独立的服务器在网络中表现为单一的系统,并以 ...

  6. BZOJ 1778: [Usaco2010 Hol]Dotp 驱逐猪猡(高斯消元+期望dp)

    传送门 解题思路 设\(f(x)\)表示到\(x\)这个点的期望次数,那么转移方程为\(f(x)=\sum\frac{f(u)*(1 - \frac{p}{q})}{deg(u)}\),其中\(u\) ...

  7. flask中的目录解析

    首先我们看一下博客blog的封面,flask和django都属于web框架.不过flask属于轻量级web框架,功能的实现是通过扩展实现的,extension.py 中存放各个对象,具体的功能通过对象 ...

  8. pythy标准库之Tkinter(hello world窗口显示)

    Tkinter :Tkinter,python内置的图形开发库GUI python3.x中: import tkinter #注意不要写成Tkinter, 一.用tkinter创建hello worl ...

  9. 自定义缓存管理器 或者 Spring -- cache

    Spring Cache 缓存是实际工作中非常常用的一种提高性能的方法, 我们会在许多场景下来使用缓存. 本文通过一个简单的例子进行展开,通过对比我们原来的自定义缓存和 spring 的基于注释的 c ...

  10. upc组队赛3 Iranian ChamPions Cup

    Iranian ChamPions Cup 题目描述 The Iranian ChamPions Cup (ICPC), the most prestigious football league in ...