My C++ Solution


  • 0
    M
    class Solution {
    

    public:
    vector<vector<int> > combine(int n, int k) {
    vector<vector<int>> result;
    if(k==0)
    {
    return result;
    }
    else if(n>k&&k==1)
    {
    int i;
    for(i=1;i<=n;i++)
    {
    vector<int> Temp;
    Temp.push_back(i);
    result.push_back(Temp);
    }
    return result;
    }
    else if(n==k)
    {
    int i;
    vector<int> Temp;
    for(i=1;i<=n;i++)
    {
    Temp.push_back(i);
    }
    result.push_back(Temp);
    return result;
    }
    else
    {
    vector<vector<int>> vec1,vec2;
    vec1 = combine(n-1,k-1);
    int i;
    for(i=0;i<vec1.size();i++)
    {
    vec1[i].push_back(n);
    }
    vec2 = combine(n-1,k);
    result.insert(result.end(),vec1.begin(),vec1.end());
    result.insert(result.end(),vec2.begin(),vec2.end());
    return result;
    }
    }
    };


Log in to reply
 

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