Given an array S of n integers, are there elements abc, 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.
  1. For example, given array S = {1 0 -1 0 -2 2}, and target = 0.
  2.  
  3. A solution set is:
  4. (-1, 0, 0, 1)
  5. (-2, -1, 1, 2)
  6. (-2, 0, 0, 2)我的答案:
  1. public class Solution {
  2. public IList<IList<int>> FourSum(int[] nums, int target) {
  3. List<IList<int>> result = new List<IList<int>>();
  4. Array.Sort(nums);
  5. ;i<nums.Length-;i++)
  6. {
  7. ||nums[i]!=nums[i-])
  8. {
  9. ;m<nums.Length-;m++)
  10. {
  11. || (nums[m]!=nums[m-]) )
  12. {
  13. ;
  14. ;
  15. ;
  16.  
  17. while(j<k)
  18. {
  19. List<int> res=new List<int>();
  20. sum=nums[i]+nums[j]+nums[k]+nums[m];
  21. if (sum==target)
  22. {
  23. ]{nums[i],nums[m],nums[j],nums[k]};
  24. res.AddRange(ints);
  25. result.Add(res);
  26. ])
  27. k--;
  28. ])
  29. j++;
  30. k--;
  31. j++;
  32. }
  33. else if (sum>target)
  34. k--;
  35. else
  36. j++;
  37. }
  38. }
  39.  
  40. }
  41. }
  42. }
  43. return result;
  44. }
  45. }

总结:

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语句中

第二种,也是效率更高更好理解的一种方法,建议掌握这种方法,因为这种方法很容易可以迁移到其他个数相加的类似问题中:

  1. public class Solution {
  2. public IList<IList<int>> FourSum(int[] nums, int target) {
  3. List<IList<int>> result = new List<IList<int>>();
  4. Array.Sort(nums);
  5. ;i<nums.Length-;i++)
  6. {
  7. int target_3=target- nums[i];
  8. ;j<nums.Length-;j++)
  9. {
  10. int target_2=target_3- nums[j];
  11. ,back=nums.Length-;
  12.  
  13. while(front<back)
  14. {
  15. int sum=nums[front]+nums[back];
  16. if(sum>target_2)
  17. back--;
  18. else if (sum<target_2)
  19. front ++;
  20. else
  21. {
  22. List<int> res=new List<int>();
  23. ]{nums[i],nums[j],nums[front],nums[back]};
  24. res.AddRange(ints);
  25. result.Add(res);
  26.  
  27. ]) front++;
  28. ]) back--;
  29. front++;
  30. back--;
  31. }
  32.  
  33. }
  34.  
  35. <nums.Length-&&nums[j]==nums[j+])
  36. j++;
  37.  
  38. }
  39. <nums.Length-&&nums[i]==nums[i+])
  40. i++;
  41. }
  42. return result;
  43. }
  44. }

C#解leetcode 18. 4Sum的更多相关文章

  1. [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 ...

  2. LeetCode——18. 4Sum

    一.题目链接:https://leetcode.com/problems/4sum/ 二.题目大意: 给定一个数组A和一个目标值target,要求从数组A中找出4个数来使之构成一个4元祖,使得这四个数 ...

  3. LeetCode 18 4Sum (4个数字之和等于target)

    题目链接 https://leetcode.com/problems/4sum/?tab=Description 找到数组中满足 a+b+c+d=0的所有组合,要求不重复. Basic idea is ...

  4. [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 ...

  5. 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 ...

  6. leetcode 15 3sum & leetcode 18 4sum

    3sum: 1 class Solution { public: vector<vector<int>> threeSum(vector<int>& num ...

  7. 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 ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. HTML部分标签的含义(3)

    1,<a>标签,链接到另一个页面 语法:<a href="目标网址" title=“鼠标滑过显示的文本”>链接显示的文本</a> title属性 ...

  2. ios的Ping++支付接入步骤-b

    1. Client 发送支付要素给 Server 2. Server 发送支付请求并将返回的支付凭据传给 Client 3. Client 调起支付控件完成支付 4. 渠道同步返回支付结果给 Clie ...

  3. 解决win8与VC++6.0不兼容问题

    找到VC++6.0安装文件夹Bin下的MSDEV.EXE程序 将MSDEV名字改为MSDEV1(或MSDEV2,3...) 右击改好的MSDEV1,打开属性面板,选择兼容性,勾上“在兼容模式下运行”, ...

  4. 将Excel导入到数据中

    常用的方式的有两种: 1. 通过 Microsoft.Jet.OLEDB.4.0 或  Microsoft.ACE.OLEDB.12.0 Microsoft.ACE.OLEDB.12.0 需要安装 A ...

  5. 关于C#的委托

    作者  陈嘉栋(慕容小匹夫) 阅读目录 0x00 前言 0x01 从观察者模式说起 0x02 向Unity3D中的SendMessage和BroadcastMessage说拜拜 0x03 认识回调函数 ...

  6. 要开始深入VMM了。

    得到一个VMM机器所有的节点状态 Quick one-liner to generate a CSV of virtual machines, sorted by their hosts. Repor ...

  7. yii分页

    关于分页有一个重要的类CPagination. CPagination represents information relevant to pagination. http://www.yiifra ...

  8. 【HDOJ】3451 Beat drop

    BFS.当水滴破裂飞溅后,直到碰到水滴才会停止(观察case1).同时,考虑当水滴飞溅到点(x,y)并且该点同一时间破裂的情况,该水滴算作吸收. /* 3451 */ #include <ios ...

  9. Linux企业级开发技术(4)——epoll企业级开发之epoll例程

    为了使大家更加深入了解epoll模型在企业应用中的使用,下面给出一段基于epoll的服务器代码,并在代码中添加了详细注释: #include <deque> #include <ma ...

  10. 数据结构,可并堆(左偏树):COGS [APIO2012] 派遣

    796. [APIO2012] 派遣 在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依据自己的工作获取报偿.  在这个帮派里,有一名忍者被称之为Master.除了Master以外,每名忍者都有且 ...