题目:

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. 【js】 ajax 与 axios 区别

    ajax 与 axios区别 Ajax: Ajax 即“Asynchronous Javascript And XML”(异步 JavaScript 和 XML),是指一种创建交互式网页应用的网页开发 ...

  2. Python-逻辑运算

    1 or 3>2 and 4<5 or 6 and 2<7

  3. PHP中的递增/递减运算符

    看这段代码 <?php $a=10; $b=++$a; //此语句等同于 ; $a=$a+1 ; $b=$a echo $a."<br>"; echo $b; ? ...

  4. 通过SSH服务登陆linux服务器(版本RHEL7)

    通过SSH服务登陆linux服务器(版本RHEL7) SSH服务概述:是一种能够以安全的方式提供远程登陆的协议,也是目前远程管理linux系统的首选方式.在此之前,我们一般使用FTP或者telnet来 ...

  5. python 3下对stm32串口数据做解析

    1.最近有个想做一个传感器数据实时显示的上位机,常规的数据打印太频繁了,无法直观的看出数据的变化. python下的上位机实现起来简单一点,网上找了一些python界面Tkinter相关资料和pyth ...

  6. ubuntu 设置环境变量

    两种方式设置环境变量: 1,添加到环境变量路径$PATH中 # 加到PATH末尾 export PATH=$PATH:/path/to/your/dir # 加到PATH开头 export PATH= ...

  7. C语言字节对齐问题详解(对齐、字节序、网络序等)

    首先说明一下,本文是转载自: http://www.cnblogs.com/clover-toeic/p/3853132.html 博客园用的少,不知道怎么发布转载文章,只能暂时这样了. 引言 考虑下 ...

  8. MyDC总结

    需要补全的代码如下 public int evaluate(String expr) { int op1, op2, result = 0; String token; StringTokenizer ...

  9. 20155320《Java程序设计》实验一(Java开发环境的熟悉)实验报告

    20155320<Java程序设计>实验一(Java开发环境的熟悉)实验报告 实验内容及步骤 (一)命令行下Java程序开发 步骤一:首先在cmd中输入d:和cd ljq20155320进 ...

  10. RHCSA-EXAM 模拟题目

    参考答案:http://www.cnblogs.com/venicid/category/1088924.html 请首先按找以下要求配置考试系统: * Hostname: server0.examp ...