Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
fair-candy-swap.cpp 464 Bytes
Copy Edit Raw Blame History
kamyu authored 2018-08-19 18:16 +08:00 . Create fair-candy-swap.cpp
// Time: O(m + n)
// Space: O(m + n)
class Solution {
public:
vector<int> fairCandySwap(vector<int>& A, vector<int>& B) {
int diff = (accumulate(A.cbegin(), A.cend(), 0) -
accumulate(B.cbegin(), B.cend(), 0)) / 2;
unordered_set<int> setA(A.begin(), A.end());
for (const auto& b: B) {
if (setA.count(b + diff)) {
return {b + diff, b};
}
}
return {};
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

Search