【一天一道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 ...
随机推荐
- Android开发之手把手教你写ButterKnife框架(三)
欢迎转载,转载请标明出处: http://blog.csdn.net/johnny901114/article/details/52672188 本文出自:[余志强的博客] 一.概述 上一篇博客讲了, ...
- 多线程(三) 实现线程范围内模块之间共享数据及线程间数据独立(ThreadLocal)
ThreadLocal为解决多线程程序的并发问题提供了一种新的思路.JDK 1.2的版本中就提供java.lang.ThreadLocal,使用这个工具类可以很简洁地编写出优美的多线程程序,Threa ...
- Unity UGUI图文混排(五) -- 一张图集对应多个Text
继上一篇说的更新了一张图集对应多个Text的功能,为了节省资源嘛 这里,但是也没有舍弃之前的一个Text一个图集,因为我感觉应该两个都有用,于是我重新写了一个脚本 1.其实大体跟前面的都没变,解析标签 ...
- Java基本语法-----java关键字
1.1关键字的概述 Java的关键字对java的编译器有特殊的意义,他们用来表示一种数据类型,或者表示程序的结构等,关键字不能用作变量名.方法名.类名.包名. 1.2常见的关键字 备注: 不必死记硬背 ...
- Nginx的负载均衡 - 加权轮询 (Weighted Round Robin) 下篇
Nginx版本:1.9.1 我的博客:http://blog.csdn.net/zhangskd 上篇blog讲述了加权轮询算法的原理.以及负载均衡模块中使用的数据结构,接着我们来看看加权轮询算法的具 ...
- 自守数算法----C语言实现
#include <stdio.h> //自守数算法 //ep : 25 ^ 2 = 625 76 ^ 2 = 5776 9376 ^ 2 = 87909376 /*ep : * 376 ...
- 2.Lucene3.6.2包介绍,第一个Lucene案例介绍,查看索引信息的工具lukeall介绍,Luke查看的索引库内容,索引查找过程
1 Lucen目录介绍 2 lucene-core-3.6.2.jar是lucene开发核心jar包 contrib 目录存放,包含一些扩展jar包 3 案例 建立第一个Lucene项目 ...
- 仿qq最新侧滑菜单
为了后续对这个项目进行优化,比如透明度动画.背景图的位移动画,以及性能上的优化. 我把这个项目上传到github上面,请大家随时关注. github地址https://github.com/sungu ...
- FFmpeg源代码简单分析:内存的分配和释放(av_malloc()、av_free()等)
===================================================== FFmpeg的库函数源代码分析文章列表: [架构图] FFmpeg源代码结构图 - 解码 F ...
- Android反编译 -- 错误代码还原
PS:如果阅读体验不好,可以尝试Github版 (<-点左边) 1. setColor(-16777216) 反编译的代码中会有很多setColor(int)的情况,比如setColor(-16 ...