c_cpp 18. 4Sum- Med - 2018.11.6

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 18. 4Sum- Med - 2018.11.6相关的知识,希望对你有一定的参考价值。

class Solution {
public:
    // 快排
    void sort(vector<int>& nums, int left, int right) {
        if (left >= right) {
            return;
        }
        int i = left;
        int j = right;
        int tmp = nums[left];
        while (i < j) {
            // j找小于tmp的数
            while (nums[j] >= tmp
                   && i < j) {
                j--;
            }
            // i找小于tmp的数
            while (nums[i] <= tmp
                   && i < j) {
                i++;
            }
            // i和j的值互换
            if (i < j) {
                int t = nums[i];
                nums[i] = nums[j];
                nums[j] = t;
            }
        }
        nums[left] = nums[i];
        nums[i] = tmp;
        sort(nums, left, i-1);
        sort(nums, i+1, right);
    }
    vector<vector<int>> fourSum(vector<int>& nums, int target) {
        vector<vector<int>> re;
        if (nums.size() < 4) {
            return re;
        }
        
        // 排序
        sort(nums, 0, nums.size()-1);
        
        map<int, vector<vector<int>>> caches;
        // 构建cache字典
        for (int i = 0; i < nums.size(); i++) {
            for (int j = i+1; j < nums.size(); j++) {
                if (!&(caches[nums[i]+nums[j]])) {
                    caches[nums[i]+nums[j]] = vector<vector<int>>();
                }
                vector<int> cacheS;
                cacheS.push_back(i);
                cacheS.push_back(j);
                caches[nums[i]+nums[j]].push_back(cacheS);
            }
        }
        
        // 匹配
        map<string, int> hash;
        for (int i = 0; i < nums.size(); i++) {
            for (int j = i+1; j < nums.size()-2; j++) {
                vector<vector<int>> cache = caches[target - nums[i] - nums[j]];
                if (!&cache) {
                    continue;
                }
                for (vector<int> cacheS : cache) {
                    // 去重
                    if (j >= cacheS[0]) {
                        continue;
                    }
                    string key = to_string(nums[i]) + '#' + to_string(nums[j]) + '#' + to_string(nums[cacheS[0]]) + '#' + to_string(nums[cacheS[1]]);
                    if (hash[key] == 1) {
                        continue;
                    }
                    hash[key] = 1;
                    
                    // append结果
                    vector<int> singleRe;
                    singleRe.push_back(nums[i]);
                    singleRe.push_back(nums[j]);
                    singleRe.push_back(nums[cacheS[0]]);
                    singleRe.push_back(nums[cacheS[1]]);

                    re.push_back(singleRe);
                }
            }
        }
        
        return re;
    }
};

以上是关于c_cpp 18. 4Sum- Med - 2018.11.6的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp 454. 4Sum II-中 - 2018.9.20

c_cpp 15. 3Sum- Med - 2018.11.1

c_cpp 5.最长的回文子串 - Med - 2018.10.15

c_cpp 8.字符串到整数atoi - Med - 2018.10.29

[leetcode-18-4Sum]

LeetCode-18-4Sum