Ejemplo n.º 1
0
    void combine(LLI &results, LI &set, int parent, int n, int k) {
        if (set.size() == k) {
            results.push_back(set);
            return;
        }

        for (int i = parent + 1; i <= n; ++i) {
            set.push_back(i);
            combine(results, set, i, n, k);
            set.pop_back();
        }
    }
Ejemplo n.º 2
0
    void search(TreeNode* root, int k1, int k2, LI &vals) {
        if (!root)
            return;

        int m = root->val;

        if (k1 < m)
            search(root->left, k1, k2, vals);

        if (k1 <= m && m <= k2)
            vals.push_back(root->val);

        if (m < k2)
            search(root->right, k1, k2, vals);
    }
Ejemplo n.º 3
0
    void combine(LLI &results, LI &set, LI &candidates, int k, int target) {
        int sum = 0;
        for (int x : set)
            sum += x;

        if (sum == target) {
            LI sorted = set;
            sort(sorted.begin(), sorted.end());
            results.push_back(sorted);
            return;
        }

        if (target < sum)
            return;

        for (int i = k; i < candidates.size(); ++i) {
            set.push_back(candidates[i]);
            combine(results, set, candidates, i, target);
            set.pop_back();
        }
    }
Ejemplo n.º 4
0
    void fourSum(LLI &res, LI &set, const LI &nums, int lo, int target) {
        int sum = 0;
        for (const int &x : set)
            sum += x;

        if (set.size() == 4 && sum == target) {
            res.push_back(set);
            return;
        }

        if (4 <= set.size())
            return;

        for (int i = lo; i < nums.size(); ++i) {
            if (i != lo && nums[i] == nums[i - 1])
                continue;

            set.push_back(nums[i]);
            fourSum(res, set, nums, i + 1, target);
            set.pop_back();
        }
    }
Ejemplo n.º 5
0
 void push_back(int val){dt.push_back(val);}