当前位置: 首页 > news >正文

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 = 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)


在数组中找到四个数和=target。


和3Sum思路一模一样,就是外面多加了一层循环。


实现代码:




public class Solution {
    public IList<IList<int>> FourSum(int[] nums, int target) 
    {
        if(nums == null || nums.Length < 4){
    		return new List<IList<int>>();
    	}
    	
    	var dic = new Dictionary<string, List<int>>();
    	var list = nums.OrderBy(x=>x).ToList();
    	
    	var len = list.Count;
    	for(var j = 0;j < len ; j++)
    	{
    		var d = list[j];
    		for (var i = 0 ;i <= len - 3 ;i++)
    		{	
    			var a = list[i];
    			var start = i+1;
    			var end = len-1;
    			if(i == j){
    				continue;
    			}
    			
    			while (start < end)
    			{
    				if(start == j){
    					start ++; 
    					continue;
    				}
    				if(end == j){
    					end --;
    					continue;
    				}
    				
    				var b = list[start];
    				var c = list[end];
    				
    				if (a+b+c+d == target) {
    
    					var v = new List<int>(){a,b,c,d}.OrderBy(x=>x).ToList();
    					var k = string.Join(",",v);
    					if(!dic.ContainsKey(k)){
    						dic.Add(k,v);
    					}
    					start ++;
    					end --;
    				}
    				else if (a+b+c+d > target){
    					end --;
    				}
    				else{
    					start ++;
    				}
    			}
    		}
    	}
    	
    	
    	var ret = new List<IList<int>>();
    	foreach(var kv in dic){
    		ret.Add(kv.Value);
    	}
    	
    	return ret;
    }
    
    
}


相关文章:

  • LeetCode -- Binary Tree Level Order Traversal II
  • (ZT)一个美国文科博士的YardLife
  • LeetCode -- Clone Graph
  • Oracle 中的nvl() 函数 相当于Sql Server 的 isnull()
  • LeetCode -- Combinations
  • [IE编程] WebBrowser控件中设置页面的缩放
  • LeetCode -- Find the Duplicate Number
  • LeetCode -- Group Anagrams
  • LeetCode -- Kth Largest Element in an Array
  • 最近评论回复汇总
  • LeetCode -- Maximum Gap
  • OO系统分析员之路--用例分析系列(8)--如何编写一份完整的UML需求规格说明书[整理重发]...
  • LeetCode -- Maximum Subarray
  • ArcGIS Server Java ADF 案例教程 25
  • LeetCode -- Minimum Depth of Binary Tree
  • [分享]iOS开发-关于在xcode中引用文件夹右边出现问号的解决办法
  • 【402天】跃迁之路——程序员高效学习方法论探索系列(实验阶段159-2018.03.14)...
  • JAVA_NIO系列——Channel和Buffer详解
  • Js基础——数据类型之Null和Undefined
  • Median of Two Sorted Arrays
  • Phpstorm怎样批量删除空行?
  • react-core-image-upload 一款轻量级图片上传裁剪插件
  • XForms - 更强大的Form
  • 腾讯优测优分享 | 你是否体验过Android手机插入耳机后仍外放的尴尬?
  • 我看到的前端
  • 新手搭建网站的主要流程
  • 做一名精致的JavaScripter 01:JavaScript简介
  • 阿里云重庆大学大数据训练营落地分享
  • !!Dom4j 学习笔记
  • # 学号 2017-2018-20172309 《程序设计与数据结构》实验三报告
  • #微信小程序(布局、渲染层基础知识)
  • $ git push -u origin master 推送到远程库出错
  • (02)vite环境变量配置
  • (DenseNet)Densely Connected Convolutional Networks--Gao Huang
  • (Redis使用系列) SpringBoot中Redis的RedisConfig 二
  • (vue)el-checkbox 实现展示区分 label 和 value(展示值与选中获取值需不同)
  • (大众金融)SQL server面试题(1)-总销售量最少的3个型号的车及其总销售量
  • (附源码)spring boot基于小程序酒店疫情系统 毕业设计 091931
  • (附源码)ssm教师工作量核算统计系统 毕业设计 162307
  • (强烈推荐)移动端音视频从零到上手(上)
  • (算法)求1到1亿间的质数或素数
  • (转)Java socket中关闭IO流后,发生什么事?(以关闭输出流为例) .
  • (转)nsfocus-绿盟科技笔试题目
  • ./include/caffe/util/cudnn.hpp: In function ‘const char* cudnnGetErrorString(cudnnStatus_t)’: ./incl
  • .Net Memory Profiler的使用举例
  • .NET Micro Framework初体验(二)
  • .NET Windows:删除文件夹后立即判断,有可能依然存在
  • .Net 代码性能 - (1)
  • @column注解_MyBatis注解开发 -MyBatis(15)
  • @for /l %i in (1,1,10) do md %i 批处理自动建立目录
  • [ C++ ] STL priority_queue(优先级队列)使用及其底层模拟实现,容器适配器,deque(双端队列)原理了解
  • [ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname
  • [3D基础]理解计算机3D图形学中的坐标系变换
  • [AIGC] SQL中的数据添加和操作:数据类型介绍
  • [cocos creator]EditBox,editing-return事件,清空输入框