2Sum 3Sum 4Sum

来源:互联网 发布:网络助手下载 编辑:程序博客网 时间:2024/04/25 08:24

一、2Sum

Two Sum

 Total Accepted: 31961 Total Submissions: 172332My Submissions

Given an array of integers, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

You may assume that each input would have exactly one solution.

Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2



从小到大排序后,一个指针从前往后,另一个指针从后往前扫,当两指针所指向的数之和大于target时,则需要将右边的指针左移,才能使它们的和变小,才有可能等于target。因为需要按下标排序,而不是输出数。所以新建了个结构体,对结构体的vector排序,比较函数要定义成static,因为成员函数默认有this指针。

class Solution {public:    typedef struct node    {        int index;        int number;                node(const int &index, const int &number)        {            this->index = index;            this->number = number;        }    }Node;        static bool comp(const Node &node1, const Node &node2)    {        return node1.number < node2.number;    }            vector<int> twoSum(vector<int> &numbers, int target) {        vector<Node> numbers_Node;        int size = numbers.size();        for (int i = 0; i < size; ++i)        {            Node node1(i + 1, numbers[i]);            numbers_Node.push_back(node1);        }            sort(numbers_Node.begin(), numbers_Node.end(), comp);        vector<int> result;        int one_index = 0, two_index = numbers_Node.size() - 1;        while (one_index < two_index)        {            if (numbers_Node[one_index].number + numbers_Node[two_index].number < target)            {                one_index++;            }            else if (numbers_Node[one_index].number + numbers_Node[two_index].number == target)            {                if (numbers_Node[one_index].index < numbers_Node[two_index].index)                {                    result.push_back(numbers_Node[one_index].index);                    result.push_back(numbers_Node[two_index].index);                }                else                {                    result.push_back(numbers_Node[two_index].index);                    result.push_back(numbers_Node[one_index].index);                }                break;            }            else            {                two_index--;            }        }        return result;    }};

二、4Sum

4Sum

 Total Accepted: 14222 Total Submissions: 65922My Submissions

Given an array S of n integers, are there elements abc, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.

Note:

  • Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d)
  • The solution set must not contain duplicate quadruplets.

    For example, given array S = {1 0 -1 0 -2 2}, and target = 0.    A solution set is:    (-1,  0, 0, 1)    (-2, -1, 1, 2)    (-2,  0, 0, 2)


深搜,有两个剪枝:排序后,如果剩余的所有数都用最小的数也比target大,则退出;如果剩余所有数都用最大的数也比target小,也退出。一个bug: 因为不包含重复的情况,所以当前放的数不能是相同的数。
<span style="font-size:18px;">class Solution {public:    vector<vector<int> > fourSum(vector<int> &num, int target) {        sort(num.begin(), num.end());        vector<vector<int> > results;        if (num.size() < 4)        {            return results;        }                vector<int> result;        DFS(0, 0, target, num, result, results);                return results;    }        void DFS(int step, int index, int target, const vector<int> &num, vector<int> result, vector<vector<int> > &results)    {        if (step >= 4)        {            if (target == 0)            {                results.push_back(result);            }            return;        }                if ((4 - step) * num[index] > target || (4 - step) * num[num.size() - 1] < target)        {            return;        }        result.push_back(num[index]);        DFS(step + 1, index + 1, target - num[index], num, result, results);                for (int i = index + 1; i <= num.size() - (4 - step); ++i)        {            if ((4 - step) * num[i] > target || (4 - step) * num[num.size() - 1] < target)            {                break;            }            if (num[i] == num[i - 1])//<span style="font-size:18px;">因为不包含重复的情况,所以当前放的数不能是相同的数。</span>            {                continue;            }                        result[step] = num[i];            DFS(step + 1, i + 1, target - num[i], num, result, results);        }    }};</span>






0 0