8ms c++ solution


  • -2
    A
    class Solution {
    public:
    	void f(vector<vector<int>>&result,int n,int k,int start,vector<int>& cur){
    		if(n-start+1<k){return;}
    		if(k==1){
    			for(int i=start;i<n;i++){
    				cur.push_back(i+1);
    				result.push_back(cur);
    				cur.pop_back();
    			}
    			return;
    		}
    		cur.push_back(start+1);
    		f(result,n,k-1,start+1,cur);
    		cur.pop_back();
    		f(result,n,k,start+1,cur);
    		return;
    	}
        vector<vector<int>> combine(int n, int k) {
             vector<vector<int>>result;
    		 vector<int>cur;
    		 f(result,n,k,0,cur);
    		 return result;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.