题目浅析

  • 想查看原题可以点击题目链接

  • 简单地说,就是给一个数组,找一种一组值相加为 0,且在多组这样的值中,选出正数最大的数值返回。

思路分享

代码解答(强烈建议自行解答后再看)

  • 参考题解
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution {
public:
int findMaxK(vector<int>& nums) {
int max_int = -1;
unordered_set<int> rec;
for (const int &i : nums) {
if (rec.count(-i)) {
max_int = max(max_int, abs(i));
}
rec.insert(i);
}
return max_int;
}
};

// class Solution {
// public:
// int findMaxK(vector<int>& nums) {
// int max_int = -1;
// int index = 0;
// unordered_map<int, int> rec;
// for (const int &i : nums) {
// if (rec.find(-i)!=rec.end() && abs(i) > max_int) {
// max_int = abs(i);
// }
// rec[i] = index++;
// }
// return max_int;
// }
// };