【一天一道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 ...
随机推荐
- CentOS7下安装GitLab
三步在CentOS7系统下,完成GitLab的安装. 1.安装和配置必须的依赖 sudo yum install curl policycoreutils openssh-server openssh ...
- Android TV开发总结(三)构建一个TV app的焦点控制及遇到的坑
转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼yuiop:http://blog.csdn.net/hejjunlin/article/details/52835829 前言:上篇中,&l ...
- Findbugs异常总汇
FindBugs是基于Bug Patterns概念,查找javabytecode(.class文件)中的潜在bug,主要检查bytecode中的bug patterns,如NullPoint空指针检查 ...
- x264源代码简单分析:宏块编码(Encode)部分
===================================================== H.264源代码分析文章列表: [编码 - x264] x264源代码简单分析:概述 x26 ...
- 4-sum问题
给定一个整数数组,判断能否从中找出4个数a.b.c.d,使得他们的和为0,如果能,请找出所有满足和为0个4个数对. #define SIZE 10 void judgeAndPut(int* arr, ...
- 没有文件扩展“.js”的脚本引擎问题解决
安装MinGW的时候提示没有文件扩展".js"的脚本引擎. 原因:系统安装Dreamwear.UltraEdit.EditPlus后修改了.js文件的默认打开方式.当想直接执行js ...
- Linux Debugging(八): core真的那么难以追踪吗?
本周遇到了好几个core都很有典型性.在这里和大家分享下. 相信有过Linux编程经验的人,肯定都遇到过.感觉周围人很多对core有天然的恐惧感,尤其对刚入行不久的同学来说.当然了,也有工作好几年看到 ...
- Android的四个基本概念(线程通信和GLSurfaceView)
GLSurfaceView提供了下列特性: 1> 管理一个surface,这个surface就是一块特殊的内存,能直接排版到android的视图view上. 2> 管理一个EGL disp ...
- 从Storm和Spark 学习流式实时分布式计算的设计
0. 背景 最近我在做流式实时分布式计算系统的架构设计,而正好又要参加CSDN博文大赛的决赛.本来想就写Spark源码分析的文章吧.但是又想毕竟是决赛,要拿出一些自己的干货出来,仅仅是源码分析貌似分量 ...
- 【Unity Shader实战】卡通风格的Shader(一)
写在前面 本系列其他文章: 卡通风格的Shader(二) 呜,其实很早就看到了这类Shader,实现方法很多,效果也有些许不一样.从这篇开始,陆续学习一下接触到的卡通类型Shader的编写. 本篇的最 ...