leetcode 40. Combination Sum II

leetcode 40. Combination Sum II

class Solution {
public:
    vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
        vector<vector<int>> result;
        vector<vector<int>> tempResult = {};
        vector<int> temp = {};
        if (candidates.size() < 1) {
            return result;
        }
        sort(candidates.begin(), candidates.end());

        int i = candidates.size() - 1;
        while (i >= 0) {
            temp.clear();
            if (target == candidates[i]) {
                while ( i >= 1 && target == candidates[i-1]) {
                    i--;
                }
                temp.push_back(target);
                result.push_back(temp);
                i--;
                continue;
            }
            if (i >= 1) {
                tempResult = combinationRangeSum(candidates, target - candidates[i], i - 1);
                if (tempResult.size()>0) {
                    for (int j = 0; j < tempResult.size(); j++) {
                        tempResult[j].push_back(candidates[i]);
                        result.push_back(tempResult[j]);
                    }
                }
            }
            i--;
        }
        map<string,bool> resultMap;
        string tempNew;
        map<string,bool>::iterator it;
        vector<vector<int>> uniqueResult;
        vector<int> tempNewResult = {};
        for(int i = 0;i<result.size();i++){
            tempNew = "";
            tempNewResult = {};
            for(int j = 0;j<result[i].size();j++){
                tempNew += string(1,(result[i][j] + '0'))+"+";
                tempNewResult.push_back(result[i][j]);
            }
            it = resultMap.find(tempNew);
            if(it==resultMap.end()){
                resultMap[tempNew] = true;
                uniqueResult.push_back(tempNewResult);
            }
        }
        return uniqueResult;

    }

    vector<vector<int>> combinationRangeSum(vector<int>& nums, int target, int right) {
        double pos = binarySearchPos(nums, target, 0, right);
        vector<vector<int>> result = {};
        vector<vector<int>> tempResult = {};
        vector<int> temp = {};
        if (pos - floor(pos)<0.4) {
            temp.push_back(nums[floor(pos)]);
            result.push_back(temp);
        }

        int i = floor(pos);
        if (i == -1 || ceil(i) == right + 1) return{};
        while (i >= 1 && target == nums[i]) {
            i--;
        }
        while (i >= 1) {
            tempResult = combinationRangeSum(nums, target - nums[i], i - 1);
            if (tempResult.size()>0) {
                for (int j = 0; j < tempResult.size(); j++) {
                    tempResult[j].push_back(nums[i]);
                    result.push_back(tempResult[j]);

                }
            }
            i--;


        }
        return result;
    }

    double binarySearchPos(vector<int>& nums1, double num, int start, int end) {
        int left = start, right = end, medium = 0;
        while (left<right) {
            medium = left + (right - left) / 2;
            if (nums1[medium] - num >0.4) {
                right = medium;
            }
            else if (num - nums1[medium] >0.4) {

                left = medium + 1;
            }
            else {
                return medium;
            }


        }
        double left1;
        if (nums1[left] - num<0.4 && num - nums1[left] <0.4)
            left1 = left;
        else if (nums1[left] - num>0.4) {
            left1 = left - 0.5;
        }
        else {
            left1 = left + 0.5;
        }
        return  left1;
    }

};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值