标签:nat 组合 cto proc 实现 protect sed roc ack
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.
For example,
If n = 4 and k = 2, a solution is:
[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ]
分析: 组合的实现,之前实现过全排列,递归求解
class Solution { public: vector<vector<int>> combine(int n, int k) { //爆搜 vector<vector<int>> res; if (k==0 || n==0) return res; for(int base=1; base<=(n-k+1); base++){ vector<int> tempres(1,base); helper(base,n,k-1,tempres,res); } return res; } void helper(int base,int n, int num, vector<int>& tempres, vector<vector<int>>& res){ if(num==0){ res.push_back(tempres); return; } if(base+num-1>n){ return; } for(int i=base+1; i<=n; i++){ tempres.push_back(i); helper(i,n,num-1,tempres,res); tempres.pop_back(); } } };
标签:nat 组合 cto proc 实现 protect sed roc ack
原文地址:http://www.cnblogs.com/willwu/p/6250585.html