LeetCode 1394. Find Lucky Integer in an Array
原题链接
简单
作者:
JasonSun
,
2020-08-12 01:05:05
,
所有人可见
,
阅读 451
Find Lucky Interger in an Array
class Solution {
public:
int findLucky(vector<int>& arr) {
const int n = std::size(arr);
const auto freq_map = [&](std::unordered_map<int, int> self = {}) {
for (int i = 0; i < n; ++i) {
self[arr[i]]++;
}
return self;
}();
const auto lucky_numbers = [&](std::vector<int> self = {}) {
for (const auto & [val, cnt] : freq_map) {
if (val == cnt) {
self.emplace_back(val);
}
}
return self;
}();
const auto solution = [&] {
if (not std::empty(lucky_numbers)) {
return *std::max_element(std::begin(lucky_numbers), std::end(lucky_numbers));
}
else {
return -1;
}
}();
return solution;
}
};