leetcode--Combinations

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],
]


使用递归和非递归方法解答。


#include<iostream>
#include<vector>
#include<stack>
#include<string>
#include<cstdlib>


using namespace std;

class Solution {
public:
    void selectk(int n, int k, int start, vector<int>&temp, vector<vector<int> >&res){  //注意参数选择,start 防止重复选择
        if(k == 0){
            res.push_back(temp);
            return;
        }
        for(int i=start; i<=n; i++){
            temp.push_back(i);
            selectk(n, k-1, i+1, temp, res);
            temp.pop_back();
        }
    }

   vector<vector<int> > combine(int n, int k) {
        vector<vector<int> >res;
        vector<int>temp;
        selectk(n, k, 1, temp,  res);
        return res;
    }

	//非递归算法选择K个元素,元素从1到N
	vector<vector<int> >combine_nonrecursion(int n, int k){
		vector<vector<int> >res;
		vector<int>temp(k);//vector 大小为K
		int i=0;
		temp[i]=0;
		while(i>=0){
			temp[i]++;
			if(temp[i] > n){
				temp[i]=0;
				i--;
			}else{
				if(i == k-1){//已经选择了k个元素
					res.push_back(temp);
				}else{
					temp[i+1]=temp[i];//保证后面元素比下面元素大,防止重复,这里直接赋值,因为while开头会增加
					i++;
				}
			}
		}
		return res;
	}

//非递归用stack实现,原理同上
	vector<stack<int> >combine_nonrecursion_stack(int n, int k){
		vector<stack<int> >res;
		//vector<int>temp;
		stack<int>st;
		int i=0;
		st.push(0);
		while(!st.empty()){
			int t=st.top();
			st.pop();

			if(t < n){
				t++;
				st.push(t);

				if(st.size() == k){
					res.push_back(st);
				}else{
					st.push(t);
				}
			}
		}
		return res;
	}
	
};


int main(){
	vector<vector<int> >res;
	int n=4, k=2;
	Solution solution;
	res=solution.combine(n, k);
	
	for(int i=0; i<res.size(); i++){
		for(int j=0; j<k; j++)
			cout<<res[i][j]<<' ';	
		cout<<endl;
	}
cout<<"*********************************&&&&&&&&&&&&&&&&&&&&&&****************************"<<endl;
	res=solution.combine_nonrecursion(n, k);
	
	for(int i=0; i<res.size(); i++){
		for(int j=0; j<k; j++)
			cout<<res[i][j]<<' ';	
		cout<<endl;
	}

cout<<"*********************************&&&&&&&&&&&&&&&&&&&&&&****************************"<<endl;
	vector<stack<int> >stres;
	stres=solution.combine_nonrecursion_stack(n, k);
	
	for(int i=0; i<stres.size(); i++){
		while(!stres[i].empty()){
			cout<<stres[i].top()<<" ";
			stres[i].pop();
		}
		cout<<endl;
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值