【一天一道LeetCode】#18. 4Sum
一天一道LeetCode
(一)题目
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
Note: Elements in a quadruplet (a,b,c,d) must be in non-descending
order. (ie, a ≤ b ≤ c ≤ d) The solution set must not contain duplicate
quadruplets.For example, given array S = {1 0 -1 0 -2 2}, and target = 0. A solution set is: (-1, 0, 0, 1) (-2, -1, 1, 2) (-2, 0, 0, 2)
(二)解题
勉强不超时的代码。(注:和3sum比较类似)
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
vector<vector<int>> result;
if(nums.size()<4) return result;
sort(nums.begin(),nums.end());
for(int i = 0 ; i < nums.size()-3 ; )
{
for(int j=i+1 ; j < nums.size()-2 ; )
{
int start = j+1;
int end = nums.size()-1;
while(start<end)
{
int sum = nums[i]+nums[j]+nums[start]+nums[end];
if(sum==target)
{
vector<int> vec;
vec.push_back(nums[i]);
vec.push_back(nums[j]);
vec.push_back(nums[start]);
vec.push_back(nums[end]);
result.push_back(vec);
start++;
while(start<end && nums[start] == nums[start-1]) start++;
end--;
while(start<end && nums[end] == nums[end+1]) end--;
}
else if(sum>target)
{
end--;
while(start<end && nums[end] == nums[end+1]) end--;
}
else {
start++;
while(start<end && nums[start] == nums[start-1]) start++;;
}
}
j++;
while(j<nums.size()-2 && nums[j] == nums[j-1]) j++;
}
i++;
while(i<nums.size()-3 && nums[i] == nums[i-1]) i++;
}
return result;
}
};
【一天一道LeetCode】#18. 4Sum的更多相关文章
- [LeetCode] 18. 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——18. 4Sum
一.题目链接:https://leetcode.com/problems/4sum/ 二.题目大意: 给定一个数组A和一个目标值target,要求从数组A中找出4个数来使之构成一个4元祖,使得这四个数 ...
- LeetCode 18 4Sum (4个数字之和等于target)
题目链接 https://leetcode.com/problems/4sum/?tab=Description 找到数组中满足 a+b+c+d=0的所有组合,要求不重复. Basic idea is ...
- [LeetCode] 18. 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 18. 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 15 3sum & leetcode 18 4sum
3sum: 1 class Solution { public: vector<vector<int>> threeSum(vector<int>& num ...
- Leetcode 18. 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 ...
- Java [leetcode 18]4Sum
问题描述: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d ...
- C#解leetcode 18. 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]18. 4Sum四数之和
Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums s ...
随机推荐
- 在安卓代码中dp 和 sp 换算px
/** * 单位转换工具 * * @author carrey * */ public class DisplayUtil { /** * 将px值转换为dip或dp值,保证尺寸大小不变 * * @p ...
- 【伯乐在线】Java线程面试题 Top 50
本文由 ImportNew - 李 广 翻译自 javarevisited.欢迎加入翻译小组.转载请见文末要求. 不管你是新程序员还是老手,你一定在面试中遇到过有关线程的问题.Java语言一个重要的特 ...
- FORM开发技术之动态控制某些item的属性
利用FORM内置函数控制ITEM包括按钮,普通ITEM等等的属性,更多内置函数学习课参考我的博客FORM内置系统函数 http://blog.csdn.net/cai_xingyun/article/ ...
- 找不到BufferedImage这个Class的解决方法
找不到BufferedImage这个Class的解决方法 环境: [1]RedHat AS5 64位 [2]WebSphere6.0 32位版本 正文: 发现原来在RedHat AS4 ...
- JAXB(Java Architecture for XML Binding)
marshal(Java对象转化成XML) import javax.xml.bind.annotation.XmlRootElement; //指定根元素,其他属性默认为根元素的子元素 @XmlRo ...
- Android简易实战教程--第二十四话《画画板》
今天完成一个画画板. 首先来个布局: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android ...
- Struts2处理流程性需求的一种解决方案
在应用程序设计中,经常出现如下的需求. 查看用户填写的数据,而且数据是分页填写. 看下面这个情况 用户的信息有三页,分别是Form abc. 现在的问题是,后面的逻辑该如何设计. 如果把,FormAB ...
- Android使用局和数据实现天气项目-android学习之旅(十二)
1.首先注册聚合数据账号,下载相应的sdk 2.导入jar包和 so文件 配置Application,初始化sdk <application //自己新建的application类 androi ...
- 剑指Offer——网易校招内推笔试题+模拟题知识点总结
剑指Offer--网易校招内推笔试题+模拟题知识点总结 前言 2016.8.2 19:00网易校招内推笔试开始进行.前天晚上利用大约1小时时间完成了测评(这个必须做,关切到你能否参与面试).上午利用2 ...
- Sharepoint2013部署ADFS 报new-sptrustedIdentityTokenIssuer:the trust provider certificate already exist
在做sharepoint2013的adfs部署时,根据MSDN的步骤到新建身份验证程序时,powershell中报"ADFS new-sptrustedIdentityTokenIssuer ...