给定一个数组,里面的是任意整数,可能有重复,再给定一个目标T,从数组中找出所有和为T的K个数,要求结果中没有重复。

Note:

  • Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, abcd)
  • 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.  
  1. 思路很容易就想到,还是利用将问题规模最小化的原则,简化问题成两个数的和为M的问题。整体的思想就是动态规划的思想了,只是特别需要注意的是要找出所有的可能。另外还需要特别处理重复和每个可能的内部顺序。4-sum的代码如下:
  1. vector<vector<int>> fourSum(vector<int>& nums, int target) {
  2. std::sort(nums.begin(), nums.end());
  3.  
  4. vector<vector<int>> ans;
  5. if (nums.size() >= 4)
  6. {
  7. findSum(ans, nums, target, 4, 0);
  8. std::set<vector<int> > dset;
  9. vector<vector<int> >::iterator itr = ans.begin();
  10. while (itr != ans.end())
  11. {
  12. std::reverse(itr->begin(), itr->end());
  13. dset.insert(*itr);
  14. ++itr;
  15. }
  16. ans.clear();
  17. std::set<vector<int> >::iterator itr2 = dset.begin();
  18. while (itr2 != dset.end())
  19. {
  20. ans.push_back(*itr2++);
  21. }
  22. }
  23. return ans;
  24. }
  25. void findSum(vector<vector<int> > &ans, vector<int>& nums, int target, int count, int start) {
  26. int sz = nums.size();
  27. if (!(count < 2 || start > sz - 1))
  28. {
  29. int p = nums[start];
  30. if (count > 2)
  31. {
  32. findSum(ans, nums, target - p, count - 1, start + 1);
  33. vector<vector<int> >::iterator itr = ans.end();
  34. while (itr !=ans.begin())
  35. {
  36. --itr;
  37. if (itr->size() == count - 1)
  38. {
  39. itr->push_back(p);
  40. }
  41. else
  42. break;
  43. }
  44. findSum(ans, nums, target, count, start + 1);
  45. }
  46. else if (count == 2)
  47. {
  48. int i = start;
  49. int j = sz - 1;
  50. while (i < j)
  51. {
  52. if (target == nums[i] + nums[j])
  53. {
  54. vector<int> v;
  55. v.reserve(4);
  56. v.push_back(nums[j]);
  57. v.push_back(nums[i]);
  58. ans.push_back(v);
  59. i++;
  60. }
  61. else if ( nums[i] + nums[j] > target)
  62. j--;
  63. else
  64. i++;
  65. }
  66. }
  67. }
  68. }

k-sum问题的更多相关文章

  1. summary of k Sum problem and solutions in leetcode

    I found summary of k Sum problem and solutions in leetcode on the Internet. http://www.sigmainfy.com ...

  2. lintcode: k Sum 解题报告

    K SUM My Submissions http://www.lintcode.com/en/problem/k-sum/ 题目来自九章算法 13% Accepted Given n distinc ...

  3. k Sum | & ||

    k Sum Given n distinct positive integers, integer k (k <= n) and a number target. Find k numbers ...

  4. 求和问题总结(leetcode 2Sum, 3Sum, 4Sum, K Sum)

    转自  http://tech-wonderland.net/blog/summary-of-ksum-problems.html 前言: 做过leetcode的人都知道, 里面有2sum, 3sum ...

  5. K Sum(2 Sum,3 Sum,4 Sum,3-Sum Closest)

    算是经典算法问题了.这里主要针对只存在一个解或者只需要求一个解的情况描述一下解题思路.若需要找到所有可能解,方法需要略作调整.如有问题,欢迎指正. 2 sum: 如果已排序,可直接用夹逼法,即两指针从 ...

  6. LeetCode解题报告--2Sum, 3Sum, 4Sum, K Sum求和问题总结

    前言: 这几天在做LeetCode 里面有2sum, 3sum(closest), 4sum等问题, 这类问题是典型的递归思路解题.该这类问题的关键在于,在进行求和求解前,要先排序Arrays.sor ...

  7. 2019年南京网络赛E题K Sum(莫比乌斯反演+杜教筛+欧拉降幂)

    目录 题目链接 思路 代码 题目链接 传送门 思路 首先我们将原式化简: \[ \begin{aligned} &\sum\limits_{l_1=1}^{n}\sum\limits_{l_2 ...

  8. 南京网络赛 E K Sum

    K Sum 终于过了这玩意啊啊啊==== 莫比乌斯反演,杜教筛,各种分块,积性函数怎么线性递推还很迷==,得继续研究研究 #include<bits/stdc++.h> using nam ...

  9. 2019南京网络赛E:K Sum

    Description: 定义函数 \[ f _n (k) = \sum _{l _1 = 1} ^n \sum _{l _2 = 1} ^n \cdots \sum _{l _k = 1} ^n \ ...

  10. Leetcode - K Sum

    List<List<Integer>> kSum_Trim(int[] a, int target, int k) { List<List<Integer>& ...

随机推荐

  1. Python语法一

    前记,今天开始学习Python 参考 笨方法学习+Python(第三版) 因为有编程基础,所以入门不难,相比于以前学过的其它语言编程,Python当然也有它独特的语法格式. 1.安装Python 访问 ...

  2. DWT小波变换及其在时间序列数据预测中的应用

    Given data: 时间序列数据. Goal:做预测 方法:在滑动窗口中取DWT特征,并验证. 实验验证: Load forcast 数据集. 问题: 小波变换的物理意义是什么? 小波变换的数学意 ...

  3. 对于那本--你必须知道的499个C语言问题--总结

    (1)1.3 (2)1.10没看懂 (3)1.11和1.12都讲到了   静态变量和局部变量,那么这两个是啥啊,我不懂: (4)1.13针对那两个字符串定义为啥有问题,不懂 (5)2.8是做什么的 ( ...

  4. 史航416第八次作业&总结

    一.知识点总结: 1.数组的输入,输出及对整个数组所有元素进行操作通常都用循环结构实现. 2.可以只给部分元素赋初值.当{ }中值的个数少于元素个数时,只给前面部分元素赋值. 3.只能给元素逐个赋值, ...

  5. postgres 批量更新内容

    在程序中遇到这样的需求, 数据库表格式如下 需要把批量更新status, 如name = fox 时, status = 1, name = boa 时,status = 2 .... 类似的 pos ...

  6. 【简易版】Java ArrayList(增删改查)

    1.什么是ArrayList ArrayList就是传说中的动态数组,用MSDN中的说法,就是Array的复杂版本,它提供了如下一些好处: (1)动态的增加和减少元素 (2)实现了ICollectio ...

  7. funny_python 00 The Zen of Python

    # 打算每天多动的时候尽量搜索一些和coding相关的funny stuff Day 00 - PEP 20 The Zen of Python 在shell里面输入python -m this 回车 ...

  8. 史上最全的Python电子书教程资源下载(转)

    网上搜集的,点击即可下载,希望提供给有需要的人^_^   O'Reilly.Python.And.XML.pdf 2.02 MB   OReilly - Programming Python 2nd. ...

  9. 在Android项目中引入MuPdf

    由于公司手机App要加入一个附件查看功能,需要查看PDF文件,在网上找了许多第三方工具,最后选择了MuPDF. 更多第三方工具可以查看大神总结的:http://www.cnblogs.com/poke ...

  10. Git学习(二)——创建版本库、查看与回退版本

    一.创建版本库 版本库,又名仓库(Repository),可以简单理解为一个目录,这个目录里的所有文件可以被Git管理起来,每个文件的修改.删除,Git都能跟踪,以便任何时刻都可以追踪历史,或者将来某 ...