LeetCode 888. Fair Candy Swap(C++)
题目:
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++)的更多相关文章
- [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 ...
- 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 ...
- 【Leetcode_easy】888. Fair Candy Swap
problem 888. Fair Candy Swap solution: class Solution { public: vector<int> fairCandySwap(vect ...
- 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 ...
- 【LeetCode】888. Fair Candy Swap 公平的糖果棒交换(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人公众号: 每日算法题 本文关键词:力扣,LeetCode,算法题,算法,Python 目录 题目描述 题目大意 解题方法 代码 刷题心得 关于作 ...
- [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 ...
- LeetCode.888-公平的糖果交换(Fair Candy Swap)
这是悦乐书的第339次更新,第363篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第208题(顺位题号是888).Alice和Bob有不同大小的糖果棒:A[i]是Alic ...
- C#LeetCode刷题之#888-公平的糖果交换(Fair Candy Swap)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3758 访问. 爱丽丝和鲍勃有不同大小的糖果棒:A[i] 是爱丽丝 ...
- [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 ...
随机推荐
- JBDC—③数据库连接池的介绍、使用和配置
首先要知道数据库连接(Connection对象)的创建和关闭是非常浪费系统资源的,如果是使用常规的数据库连接方式来操作数据库,当用户变多时,每次访问数据库都要创建大量的Connnection对象,使用 ...
- js随笔--关于数组
1.split()将一个字符串分割成字符串数组 stringObject.split(separator,howmany) separator:必需,字符串或正则表达式,从该参数指定的地方分割stri ...
- 微信JSSDK的使用
微信JS-SDK 1.在微信公众平台(https://mp.weixin.qq.com/)注册个公众号,获取APPID和AppSecret 2.获取access_token(需要在公众平台中设置获取a ...
- Ubuntu操作系统(我的是ubuntu 18.04.3 LTS)
查看是否开启了ssh服务是否安装,使用命令: sudo ps -e |grep ssh 如果安装了的是会有sshd服务的,下面的图片是没有安装ssh服务 2 先更新资源列表,使用命令: sudo ap ...
- shell习题第7题:备份数据库
[题目要求] 设计一个shell脚本用来备份数据库,首先在本地服务器上保存一份数据,然后再远程拷贝一份,本地保存一周的数据,远程保存一个月 假设我们知道mysql root账号的密码,要备份的库为da ...
- java业务逻辑,利用hibernate获取所连接的数据库信息
1.本人程序架构是springMVC+hibernate,这次的需求是要针对不同的数据库,做不同的处理. 2.获取所连接的数据库是什么,oracle? mysql? sql server? 基础 ...
- Redis事件
Redis事件 Redis的ae(Redis用的事件模型库) ae.c Redis服务器是一个事件驱动程序,服务器需要处理以下两类事件: 文件事件(file event):Redis服务器通过套接字与 ...
- 将http转为https访问
1.去阿里云购买证书 有免费一年的证书 最多20个 一个证书需要填写一个二级域名 www.xxx.com2.开启apache相应配置 #修改httpd.conf文件 LoadModule ssl_mo ...
- yii学习笔记(2),创建控制器
将网站根目录配置到项目的web目录 打开网站访问的是web/index.php这时打开默认页面 访问一下其他页面,发现浏览器地址的url携带了一个参数 http://www.test.com/inde ...
- 将Windows 7安装到移动固态硬盘(U盘)
由于工作的原因,有时需要用两台电脑工作,而搞开发的人大多又是追求计算机性能的人,所以笔记本电脑自然不轻,更何况两台. 两台电脑折磨我半年多,终于下定决心将个系统安装到移动固态硬盘中,背一台无硬盘电脑加 ...