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 ...
随机推荐
- 设计模式——如何避免在OO设计中违反依赖倒置原则
1 变量不可以包含具体类的引用.一旦new,就对具体类产生依赖,用工厂模式来避开. 2 类不要派生至具体类.用派生抽象类避开. 3 不要覆盖基类已经实现的方法.基类中已实现的方法应该由所有子类共享.
- delphi OnMouseLeave 事件不灵敏及解决之道(使用TrackMouseEvent函数进行加强)
http://topic.csdn.net/t/20020104/09/456913.html CM_MouseLeave消息好象不太灵敏,当鼠标快速移出窗体时,就收不到这个消息,请问大家有什么好办法 ...
- Linux Kernel 'MSR' Driver Local Privilege Escalation
本站提供程序(方法)可能带有攻击性,仅供安全研究与教学之用,风险自负! // PoC exploit for /dev/cpu/*/msr, 32bit userland on a 64bit hos ...
- nginx 1.3.9/1.4.0 x86 Brute Force Remote Exploit
测试方法: 本站提供程序(方法)可能带有攻击性,仅供安全研究与教学之用,风险自负! #nginx 1.3.9/1.4.0 x86 brute force remote exploit # copyri ...
- (转载)用PHP正则表达式清除字符串的空白
(转载)http://www.chinaz.com/program/2009/0220/67569.shtml 我们经常会处理来自用户输入或从数据库中读取的数据,可能在你的字符串中有多余的空白或制表符 ...
- 杂题 SPOJ MOBILE2 - Mobiles
MOBILE2 - Mobiles no tags You have been asked to buy a gift for your baby brother, Ike. However, yo ...
- sql server 2008有关SQL的模糊查询
执行 数据库查询时,有完整查询和模糊查询之分. 一般模糊语句如下: SELECT 字段 FROM 表 WHERE 某字段 Like 条件 其中关于条件,SQL提供了四种匹配模式: 1,%:表示任意0个 ...
- HDOJ 2056 Rectangles
Problem Description Given two rectangles and the coordinates of two points on the diagonals of each ...
- POJ 2057 The Lost House
题意:一只蜗牛,它的房子在树上的某个叶子节点上,它要从树的根节点出发,寻找自己的房子.树的任意两个节点的距离为1,房子出现在每个叶子节点上的可能性一样.有的节点上有虫子,如果有虫子,虫子会告诉蜗牛它的 ...
- hdu 4424 & zoj 3659 Conquer a New Region (并查集 + 贪心)
Conquer a New Region Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...