题目链接

【题解】

两重循环枚举[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. cerebro使用

    一.安装cerebro 上传插件到任意路径 tar zxvf 解压插件包 [root@ngsocdev14 es]# ls cerebro-0.8.3.zip software [root@ngsoc ...

  2. Python基础(二):斐波那契数列、模拟cp操作、生成8位随机密码

    一.斐波那契数列 目标: 编写fib.py脚本,主要要求如下: 输出具有10个数字的斐波那契数列 使用for循环和range函数完成 改进程序,要求用户输入一个数字,可以生成用户需要长度的斐波那契数列 ...

  3. WebRequest发送请求并接收返回值

    public string getXmlStr(string hphmcode)         {            string Url = "http://localhost:80 ...

  4. layui-treeTable v2.0添加搜索功能

    layui-treeTable 添加搜索功能 在树形表格头部加一个input框: <div class="layui-inline"> <input class= ...

  5. 20175223 MySQL

    目录 完成结果 要求 1 :导入world.sql 要求 2 :CityWanna.java CityWanna.java 要求 3 :CountryWanna.java CountryWanna.j ...

  6. 项目集成swagger,并暴露指定端点给swagger

    项目集成swagger 一:思考: 1.swagger解决了我们什么问题? 传统开发中,我们在开发完成一个接口后,为了测试我们的接口,我们通常会编写单元测试,以测试我们的接口的可用性,或者用postm ...

  7. WebBrowser是IE内置的浏览器控件

    WebBrowser是IE内置的浏览器控件.WebBrowser是IE内置的浏览器控件.WebBrowser是IE内置的浏览器控件.重要的事情说三遍,原因是一开始使用的时候就在这踩了坑. WebBro ...

  8. HDU 1724 Ellipse (自适应辛普森积分)

    题目链接:HDU 1724 Problem Description Math is important!! Many students failed in 2+2's mathematical tes ...

  9. SCI小论文投稿记录

    英文小论文投的是SCI 3区的一个刊物,收录在spring,ei等, 投稿的时候2019/2/3影响因子2.8左右 现在2019/8/13  影响因子3.844 先科普下论文的各个状态 1. Subm ...

  10. python学习笔记:json与字典的转换(dump 和dumps load和loads的区别)

    1. json序列化(字典转成字符串)方法: dumps:无文件操作            dump:序列化+写入文件 2. json反序列化(字符串转成字典)方法: loads:无文件操作     ...