My c++ dp solution


  • 1
    B
    class Solution {
    public:
        vector<vector<int> > combine(int n, int k) {
            
            vector<vector<int> > F[n+1][n+1];
            for ( int i = 1; i <= n; i++ ) {
                for ( int j = 1; j <= i; j++ ) {
                    F[i][j] = F[i-1][j];
                    if ( F[i-1][j-1].size() == 0 ) {
                        vector<int> temp;
                        temp.push_back(i);
                        F[i][j].push_back(temp);
                    }
                    else {
                        for ( int k = 0; k < F[i-1][j-1].size(); k++ ) {
                            vector<int> temp = F[i-1][j-1][k];
                            temp.push_back(i);
                            F[i][j].push_back(temp);
                        }
                    }
                }
            }
            return F[n][k];
        }
    };

Log in to reply
 

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