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 = 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)我的答案:
public class Solution {
public IList<IList<int>> FourSum(int[] nums, int target) {
List<IList<int>> result = new List<IList<int>>();
Array.Sort(nums);
;i<nums.Length-;i++)
{
||nums[i]!=nums[i-])
{
;m<nums.Length-;m++)
{
|| (nums[m]!=nums[m-]) )
{
;
;
;
while(j<k)
{
List<int> res=new List<int>();
sum=nums[i]+nums[j]+nums[k]+nums[m];
if (sum==target)
{
]{nums[i],nums[m],nums[j],nums[k]};
res.AddRange(ints);
result.Add(res);
])
k--;
])
j++;
k--;
j++;
}
else if (sum>target)
k--;
else
j++;
}
}
}
}
}
return result;
}
}
总结:
1 这个答案虽然可以Accepted,但是运行时间太慢
2 int[] ints=new int[4]{nums[i],nums[m],nums[j],nums[k]}; res.AddRange(ints); 利用这两句可以向List中批量添加数据
3 Array.Sort(nums);可以直接对数组排序。
4 这个算法是在sum3的基础上改进的,在第二层for循环中要有条件(m-i)==1;确保当i!=0时候,就算m=i+1&&nums[m]==nums[i]的时候,也能进入if语句中
第二种,也是效率更高更好理解的一种方法,建议掌握这种方法,因为这种方法很容易可以迁移到其他个数相加的类似问题中:
public class Solution {
public IList<IList<int>> FourSum(int[] nums, int target) {
List<IList<int>> result = new List<IList<int>>();
Array.Sort(nums);
;i<nums.Length-;i++)
{
int target_3=target- nums[i];
;j<nums.Length-;j++)
{
int target_2=target_3- nums[j];
,back=nums.Length-;
while(front<back)
{
int sum=nums[front]+nums[back];
if(sum>target_2)
back--;
else if (sum<target_2)
front ++;
else
{
List<int> res=new List<int>();
]{nums[i],nums[j],nums[front],nums[back]};
res.AddRange(ints);
result.Add(res);
]) front++;
]) back--;
front++;
back--;
}
}
<nums.Length-&&nums[j]==nums[j+])
j++;
}
<nums.Length-&&nums[i]==nums[i+])
i++;
}
return result;
}
}
C#解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 ...
- [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 ...
随机推荐
- import uno 错误
安装aeroolib 模块后,提示没有 uno 相关段一些模块, 原因是这些模块是 openoffice 中段,需要先安装 openoffice. 1:清除所有 libreoffice 软件, su ...
- Lambda表达式中的表达式lambda和语句lambda区别
Lambda表达式可分为表达式lambda和语句lambda 表达式lambda:表达式位于 => 运算符右侧的lambda表达式称为表达式lambda (input parameters) = ...
- BZOJ 1143 祭祀
Description 在遥远的东方,有一个神秘的民族,自称Y族.他们世代居住在水面上,奉龙王为神.每逢重大庆典, Y族都会在水面上举办盛大的祭祀活动.我们可以把Y族居住地水系看成一个由岔口和河道组成 ...
- Network Saboteur
poj2531:http://poj.org/problem?id=2531 题意:给你一个图,图中点之间会有边权,现在问题是把图分成两部分,使得两部分之间边权之和最大.题解:一开始比知道怎么办,想用 ...
- Javascript面向对象编程(二):构造函数的继承 by 阮一峰
今天要介绍的是,对象之间的"继承"的五种方法. 比如,现在有一个"动物"对象的构造函数. function Animal(){ this.species = & ...
- (转载)PHP使用empty检查函数返回结果时报Fatal error: Can't use function return value in write context的问题
(转载)http://be-evil.org/post-153.html PHP开发时,当你使用empty检查一个函数返回的结果时会报错:Fatal error: Can't use function ...
- 图论(对偶图):COGS 470. [NOI2010]海拔
470. [NOI2010]海拔 ★★★☆ 输入文件:altitude.in 输出文件:altitude.out 简单对比 时间限制:2 s 内存限制:512 MB 海拔 [问题描述] ...
- Selenium自动化测试环境搭建汇总(一):Selenium+Eclipse+Junit+TestNG
第一步 安装JDK JDk1.7. 下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-188026 ...
- QTP自传之录制
录制,是一件吃力不讨好的活.很多人以为录制就是我的主要甚至全部的功能,这是大错特错的.不过,录制功能却是不熟悉我的人了解我的有效途径,是大家学习的有力武器.今天就先从录制功能说起吧. 说到录制,就不得 ...
- Unable to load native-hadoop library解决思路
最近试着搭建Hadoop,我使用的操作系统是Centos6.5,Hadoop版本是2.6.0. 在安装过程中总是出现:WARN util.NativeCodeLoader: Unable to loa ...