【LeetCode 18】四数之和
【题解】
两重循环枚举[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】四数之和的更多相关文章
- Java实现 LeetCode 18 四数之和
18. 四数之和 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target ...
- LeetCode 18. 四数之和(4Sum)
题目描述 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等? ...
- [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 ...
- [LeetCode] 18. 四数之和
题目链接:https://leetcode-cn.com/problems/4sum/ 题目描述: 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个 ...
- LeetCode:四数之和【18】
LeetCode:四数之和[18] 题目描述 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c ...
- 【LeetCode】18.四数之和
题目描述 18. 四数之和 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 t ...
- 代码随想录第七天| 454.四数相加II、383. 赎金信 、15. 三数之和 、18. 四数之和
第一题454.四数相加II 给你四个整数数组 nums1.nums2.nums3 和 nums4 ,数组长度都是 n ,请你计算有多少个元组 (i, j, k, l) 能满足: 0 <= i, ...
- 【LeetCode】四数之和
[问题]给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找 ...
- [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 ...
- 【LeetCode】四数之和【排序,固定k1,k2,二分寻找k3和k4】
给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满 ...
随机推荐
- 检测ip是否通过
#!/bin/bashnetstat -an |grep "ESTABLISHED" |awk '{print $4}' |awk -F ':' '{print $1}' |sor ...
- SQL Server数据库备份&还原
一.备份 1.登录数据库 2.找到要还原的数据库 右键-任务-备份-添加(路径只写一个,刚开始二个总是报错)-确定 二.还原数据库 这个之间报错了二次 1.报错1:备份集中的数据库与现有数据库“XXX ...
- Android Studio的Gradle错误解决方法
因为喜欢尝鲜,试了一下google的Android studio,但是在创建工程时,出现了尝试连接service.gradle.org错误的问题,查了一下,在StackOverFlow找到以下答案: ...
- 2018icpc南京/gym101981 G Pyramid 找规律
题意: 数一个金字塔里面有多少个正三角形. 题解: ans[n]=n*(n-1)*(n-2)*(n-3)/24 #include<bits/stdc++.h> using namespac ...
- Linux用ifconfig设置IP、掩码、网关
ifconfig eth0 ip netmask 255.255.255.0 route add default gw 网关
- c#网络通信框架networkcomms内核解析之一 消息传送
networkcomms.net 来自英国的网络通信框架 官方网址 www.networkcomms.net 中文网址www.networkcomms.cn 在网络通信程序中,本地的类或者对象,要传输 ...
- css3水平垂直居中(不知道宽高同样适用)
css水平垂直居中 第一种方法: 在父div里加: display: table-cell; vertical-align: middle; text-align: center; 内部div设置: ...
- java 并发——ReentrantLock
java 并发--ReentrantLock 简介 public class ReentrantLock implements Lock, java.io.Serializable { // 继承了 ...
- Java对象toString()方法
对象的字符串表示以可读格式包含有关对象状态的信息.Object类的toString()方法表示字符串中类的对象.Object类提供了toString()方法的默认实现. 它返回一个以下格式的字符串: ...
- Scrapy框架: pipelines.py设置
保存数据到json文件 # -*- coding: utf-8 -*- # Define your item pipelines here # # Don't forget to add your p ...