[LeetCode] 4Sum

算法渣,现实基本都参考或者完全拷贝[戴方勤([email protected])]大神的leetcode题解,此处仅作刷题记录。

 

 1 class Solution {

 2 public:

 3     vector<vector<int>  >  fourSum(vector<int>  &num, int  target)  {

 4         vector<vector<int> > result;

 5         if (num.size() < 4)

 6             return result;

 7         sort(num.begin(), num.end());

 8         

 9         unordered_map<int, vector<pair<int, int> > > cache;

10         for (size_t a = 0; a < num.size(); a++){

11             for (size_t b = a + 1; b < num.size(); b++) {

12                 cache[num[a] + num[b]].push_back(pair<int, int>(a, b)); // 对于某个和,可能存在多种组合,这些组合存储到一个向量中

13             }

14         }

15 

16         for (size_t c = 0; c < num.size(); c++) {

17             for (size_t d = c + 1; d < num.size(); d++) {

18                 const int key = target - num[c] - num[d];

19                 if (cache.find(key) == cache.end())

20                     continue;

21 

22                 const auto& vec = cache[key];

23                 for (size_t k = 0; k < vec.size(); ++k) {

24                     if (c <= vec[k].second) // 重复

25                         continue;

26                     result.push_back({ num[vec[k].first], num[vec[k].second], num[c], num[d] });

27                 }

28             }

29         }

30 

31         sort(result.begin(), result.end());

32         result.erase(unique(result.begin(), result.end()), result.end());

33         return result;

34     }

35 };

 

下面方法感觉更好

 1 class  Solution  {

 2 public:

 3     vector<vector<int>>  fourSum(vector<int>&  num, int  target)  {

 4         vector<vector<int>>  result;

 5         if (num.size()  <  4)  return  result;

 6         sort(num.begin(), num.end());

 7         unordered_multimap<int, pair<int, int>>  cache;

 8         for (int i = 0; i + 1 < num.size(); ++i)

 9         for (int j = i + 1; j < num.size(); ++j)

10             cache.insert(make_pair(num[i] + num[j], make_pair(i, j)));

11         for (auto i = cache.begin(); i != cache.end(); ++i)  {

12             int  x = target - i->first;

13             auto  range = cache.equal_range(x);

14             for (auto j = range.first; j != range.second; ++j)  {

15                 auto  a = i->second.first;

16                 auto  b = i->second.second;

17                 auto  c = j->second.first;

18                 auto  d = j->second.second;

19                 if (a != c  &&  a != d  &&  b != c  &&  b != d)  {

20                     vector<int>  vec = { num[a], num[b], num[c], num[d] };

21                     sort(vec.begin(), vec.end());

22                     result.push_back(vec);

23                 }

24             }

25         }

26         sort(result.begin(), result.end());

27         result.erase(unique(result.begin(), result.end()), result.end());

28         return  result;

29     }

30 };

 

杂记:

1. pair

This class couples together a pair of values, which may be of different types (T1 and T2). The individual values can be accessed through its public members first and second.

Pairs are a particular case of tuple.

2. 其他方法用到的类 unordered_multimap

Unordered multimaps are associative containers that store elements formed by the combination of a key value and amapped value, much like unordered_map containers, but allowing different elements to have equivalent keys.

Elements with equivalent keys are grouped together in the same bucket and in such a way that an iterator (seeequal_range) can iterate through all of them.

3. insert

unordered_multimap没有[]运算符,必须以insert方式插入数据,而且插入式必须调用make_pair函数

4. equal_range

Get subrange of equal elements

Returns the bounds of the subrange that includes all the elements of the range [first,last) with values equivalent tova

你可能感兴趣的:(LeetCode)