给定一个数组,里面的是任意整数,可能有重复,再给定一个目标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.
    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)
 
思路很容易就想到,还是利用将问题规模最小化的原则,简化问题成两个数的和为M的问题。整体的思想就是动态规划的思想了,只是特别需要注意的是要找出所有的可能。另外还需要特别处理重复和每个可能的内部顺序。4-sum的代码如下:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
std::sort(nums.begin(), nums.end()); vector<vector<int>> ans;
if (nums.size() >= 4)
{
findSum(ans, nums, target, 4, 0);
std::set<vector<int> > dset;
vector<vector<int> >::iterator itr = ans.begin();
while (itr != ans.end())
{
std::reverse(itr->begin(), itr->end());
dset.insert(*itr);
++itr;
}
ans.clear();
std::set<vector<int> >::iterator itr2 = dset.begin();
while (itr2 != dset.end())
{
ans.push_back(*itr2++);
}
}
return ans;
}
void findSum(vector<vector<int> > &ans, vector<int>& nums, int target, int count, int start) {
int sz = nums.size();
if (!(count < 2 || start > sz - 1))
{
int p = nums[start];
if (count > 2)
{
findSum(ans, nums, target - p, count - 1, start + 1);
vector<vector<int> >::iterator itr = ans.end();
while (itr !=ans.begin())
{
--itr;
if (itr->size() == count - 1)
{
itr->push_back(p);
}
else
break;
}
findSum(ans, nums, target, count, start + 1);
}
else if (count == 2)
{
int i = start;
int j = sz - 1;
while (i < j)
{
if (target == nums[i] + nums[j])
{
vector<int> v;
v.reserve(4);
v.push_back(nums[j]);
v.push_back(nums[i]);
ans.push_back(v);
i++;
}
else if ( nums[i] + nums[j] > target)
j--;
else
i++;
}
}
}
}

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. php使用curl简单抓取远程url的方法

    这篇文章主要介绍了php使用curl简单抓取远程url的方法,涉及php操作curl的技巧,具有一定参考借鉴价值,需要的朋友可以参考下     本文实例讲述了php使用curl抓取远程url的方法.分 ...

  2. 【英语魔法俱乐部——读书笔记】 1 初级句型-简单句(Simple Sentences)

    第一部分 1 初级句型-简单句(Simple Sentences):(1.1)基本句型&补语.(1.2)名词短语&冠词.(1.3)动词时态.(1.4)不定式短语.(1.5)动名词.(1 ...

  3. Office 365 系列一 ------- 如何单个安装Office 客户端和Skype for business

    当我们注册好或者购买好 Office 365后,我们的单个用户如何进行在线的.流式的方式安装好我们的客户端,特别是对于我们非IT部门来说,这是一个比较为难的事情, 经常需要我们的IT去到同事的电脑旁边 ...

  4. hg常用命令

    关于hg命令选项 如果你是在windows系统下,使用的是图像界面,你很可能不常用它.但是一旦你了解这些命令之后,会觉得很方便.hg有很多命令,这些命令都有一定的选项,在开始的时候,只知道用它,有时候 ...

  5. webstorm 2016 激活(转)

    2016.2.2 版本的破解方式: 安装以后,打开软件会弹出一个对话框:选择"license server" 输入:http://114.215.133.70:41017 2016 ...

  6. asp.net LINQ LinqDataSource控件显示数据和DropdownList显示数据

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  7. docker安装

    系统要求:需要一个64位的centos7操作系统和版本3.10或更高版本的Linux内核 开始安装: uname -r   //查看内核版本yum -y update //更新系统更新到最新 #安装d ...

  8. spring mvc 4.3.2 + mybatis 3.4.1 + mysql 5.7.14 +shiro 幼儿园收费系统 之 动态组合条件查询

    实际应用中,系统设计无法预料到用户最终的查询条件是怎样的.通常的做法是给出一些限制死的查询条件让用户查询.业务稍有改动,就要重新设计界面,增加查询字段等,费时费力. 比较好的做法是,除了常用的查询外, ...

  9. pip install lxml出错解决

    初学Python各种版本问题,安装pip install lxml各种出错,解决方法:py -2 -m pip install wheel(PY3上我上个帖子已经标了),http://www.lfd. ...

  10. Activity启动模式

    ------siwuxie095 共4种启动模式:standard singleTop singleTask singleInstance 1.标准启动模式(standard) 也即默认的启动模式 ( ...