题目:

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. c++学习笔记(新手学习笔记,如有错误请与作者联系)

    逗号”,“运算符:a = 公式1,公式2:把公式1的结果放进公式2中进行运算,如: a = 3*5 , a*4; 计算结果:a = 3*5*4=60; typedef:类型别名,为已有类型另外命名 t ...

  2. python3爬虫-通过requests获取拉钩职位信息

    import requests, json, time, tablib def send_ajax_request(data: dict): try: ajax_response = session. ...

  3. python的requests模块爬取网页内容

    注意:处理需要用户名密码认证的网站,需要auth字段. # -*- coding:utf-8 -*- import requests headers = { "User-Agent" ...

  4. jqGrid 分页

    这两天一直在搞jqGrid分页,焦头烂额,不过还是有点收获的(主要是后台分页):   jqGrid分页可以分为两种,远程数据(服务器数据)分页和本地数据分页,     先看远程数据分页:   $(&q ...

  5. decodeURI、decodeURIComponent 编码方法

    ——摘自<JavaScript高级程序设计> 编码: Global 对象的 encodeURI()和 encodeURIComponent()方法可以对 URI(Uniform Resou ...

  6. [教学] Delphi IDE 文件搜寻功能

    Delphi IDE 提供了一个方便的文件搜寻功能,操作如下: 点 Search 选单内的 Find in Files... 例如我们想搜寻 JFile 需要引用那一个源码,可输入如下: 输入关键字: ...

  7. 基于STM32的简易磁卡充值系统

    使用的是MFRC522射频模块,把磁卡放入感应区后,可以执行三种操作: 初始化磁卡金额 读取卡内金额 向卡内写入金额(充值) 本来想着回学校了能把洗浴卡的金额给改掉,实现帝皇般的尊贵洗浴享受(不花钱… ...

  8. python 新手题目-文件读写-关键字判断

    import os import json import re filename ="D:/pytest/2000/100/111.txt" f = open(filename,& ...

  9. 利用Docker设置Node.js

      docker是一个开源的应用容器引擎,可以为我们提供安全.可移植.可重复的自动化部署的方式.docker采用虚拟化的技术来虚拟化出应用程序的运行环境.如上图一样.docker就像一艘轮船.而轮船上 ...

  10. 海面波浪模拟 MATLAB

    数学建模美赛集训的时候要用到一个海面模拟,分享一下海面模拟的MATLAB代码 先贴一下结果图: 下面是源代码~~~ function waterwave n = 64; % grid size g = ...