Leetcode 77. combination

来源:互联网 发布:去外企工作好吗 知乎 编辑:程序博客网 时间:2024/06/03 22:47

Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.

Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.

Note:
You are not suppose to use the library’s sort function for this problem.

click to show follow up.

class Solution {public:    vector<vector<int>> combine(int n, int k) {        vector<vector<int> > ret;        vector<int> ans;        reCombine(ret, ans, n, k);        return ret;    }    void reCombine(vector<vector<int> >& ret, vector<int>& ans, int n, int k) {        size_t sz = ans.size();        if (k == 0) {            ret.push_back(ans);            return;        }        size_t start;        if (sz == 0) start = 1;        else start = ans[sz - 1] + 1;        for (size_t i = start; i <= n - k + 1; ++i) {            ans.push_back(i);            reCombine(ret, ans, n, k - 1);            ans.pop_back();        }    }};