题目:

Alice and Bob have candy bars of different sizes: A[i] is the size of the i-th bar of candy that Alice has, and B[j] is the size of the j-th bar of candy that Bob has.

Since they are friends, they would like to exchange one candy bar each so that after the exchange, they both have the same total amount of candy.  (The total amount of candy a person has is the sum of the sizes of candy bars they have.)

Return an integer array ans where ans[0] is the size of the candy bar that Alice must exchange, and ans[1] is the size of the candy bar that Bob must exchange.

If there are multiple answers, you may return any one of them.  It is guaranteed an answer exists.

Example 1:

Input: A = [1,1], B = [2,2]
Output: [1,2]

Example 2:

Input: A = [1,2], B = [2,3]
Output: [1,2]

Example 3:

Input: A = [2], B = [1,3]
Output: [2,3]

Example 4:

Input: A = [1,2,5], B = [2,4]
Output: [5,4]

分析:

两个人拥有一些糖果,交换一个后,两人糖果总和相等。

我们计sumA为A拥有的和,sumB为B拥有的和,a是A交出去的糖果,b是B交换出去的糖果,根据题意有如下等式。

sumA-a+b=sumB-b+a  =>  sumB-sumA=2b-2a  =>  b-a=(sumB-sumA)/2

我们只需要找到一组满足b-a=(sumB-sumA)/2的b和a,交换后两人的糖果总和便相等了。

可以使用map存储A糖果的个数,然后遍历B的时候,查找map[(sumB-sumA)/2-b]是否等于1就可以了,实现方法有很多,这里就不一一赘述了。

程序:

class Solution {
public:
vector<int> fairCandySwap(vector<int>& A, vector<int>& B) {
vector<int> res;
int sumA = ;
int sumB = ;
for(auto a:A){
sumA += a;
}
for(auto b:B){
sumB += b;
}
int cha = (sumB - sumA)/;
unordered_map<int, int> m;
for(auto a:A){
m[a] = ;
}
for(auto b:B){
if(m[b-cha] == ){
res.push_back(b-cha);
res.push_back(b);
return res;
}
}
return res;
}
};

LeetCode 888. Fair Candy Swap(C++)的更多相关文章

  1. [LeetCode] 888. Fair Candy Swap 公平糖果交换

    Alice and Bob have candy bars of different sizes: A[i] is the size of the i-th bar of candy that Ali ...

  2. LeetCode 888 Fair Candy Swap 解题报告

    题目要求 Alice and Bob have candy bars of different sizes: A[i] is the size of the i-th bar of candy tha ...

  3. 【Leetcode_easy】888. Fair Candy Swap

    problem 888. Fair Candy Swap solution: class Solution { public: vector<int> fairCandySwap(vect ...

  4. 888. Fair Candy Swap@python

    Alice and Bob have candy bars of different sizes: A[i] is the size of the i-th bar of candy that Ali ...

  5. 【LeetCode】888. Fair Candy Swap 公平的糖果棒交换(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人公众号: 每日算法题 本文关键词:力扣,LeetCode,算法题,算法,Python 目录 题目描述 题目大意 解题方法 代码 刷题心得 关于作 ...

  6. [LeetCode&Python] Problem 888. Fair Candy Swap

    Alice and Bob have candy bars of different sizes: A[i] is the size of the i-th bar of candy that Ali ...

  7. LeetCode.888-公平的糖果交换(Fair Candy Swap)

    这是悦乐书的第339次更新,第363篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第208题(顺位题号是888).Alice和Bob有不同大小的糖果棒:A[i]是Alic ...

  8. C#LeetCode刷题之#888-公平的糖果交换(Fair Candy Swap)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3758 访问. 爱丽丝和鲍勃有不同大小的糖果棒:A[i] 是爱丽丝 ...

  9. [Swift]LeetCode888. 公平的糖果交换 | Fair Candy Swap

    Alice and Bob have candy bars of different sizes: A[i] is the size of the i-th bar of candy that Ali ...

随机推荐

  1. #leetcode刷题之路28-实现 strStr() 函数

    实现 strStr() 函数.给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存在,则返回 ...

  2. SessionStroage和locationStorage的思考

    从理论上讲 LocalStroage 内存更大,存储时间更为持久,作用域更大.那么SessionStroage有存在的必要吗?有什么样的应该场景是必须用seessionStroage 而不能使用Loc ...

  3. 在UIWindow上加类似于“回到顶部”的按钮

    在公司上个版本的开发中遇到了一个UI布局的小问题: 某个页面需要增加一个分享按钮,但是该页面是二级页面,导航栏右边也已经放置了2个button. 起初和老大谈论这个问题的时候想到的方法是导航栏右边加三 ...

  4. ES6学习之-let 和const命令

    let 和const命令 let命令 用来声明变量,类似于var .let声明的变量 只在let命令所在的代码块内有效. 在for循环里也是如此 每次循环其实都是一个代码块 function fn() ...

  5. python面试题之基础2

    2.3 考虑以下 Python 代码,如果运行结束,命令行中的运行结果是什么? 两者用法相同,不同的是 range 返回的结果是一个列表,而 xrange 的结果是一个生成器,前者是 直接开辟一块内存 ...

  6. 03.搭建Spark集群(CentOS7+Spark2.1.1+Hadoop2.8.0)

    接上一篇:https://www.cnblogs.com/yjm0330/p/10077076.html 一.下载安装scala 1.官网下载 2.spar01和02都建立/opt/scala目录,解 ...

  7. PMP考试通过

    经过3个月的努力,终于在10月8号,过完国庆假期,得知考试通过.虽然没有得到5A,只有4A,心也算落下了.备考的过程中,通过学习小组讨论,互相交流,辅导. 自己也对学习的知识加深印象.总结一下整个学习 ...

  8. Redis数据结构总结

    Redis 字符串(String) SET runoobkey redis GET runoobkey Redis 哈希(Hash) Redis hash 是一个string类型的field和valu ...

  9. 实验三 敏捷开发与XP实践-1

    实验内容 XP基础 XP核心实践 相关工具 实验要求 1.没有Linux基础的同学建议先学习<Linux基础入门(新版)><Vim编辑器> 课程 2.完成实验.撰写实验报告,实 ...

  10. 20155332 2006-2007-2 《Java程序设计》第2周学习总结

    20155332 2006-2007-2 <Java程序设计>第2周学习总结 教材学习内容总结 在JAVA中分为基本类型和类类型两大类型系统. JAVA中的基本类型主要分为如下几类: 1. ...