Leetcode888.Fair Candy Swap公平的糖果交换
爱丽丝和鲍勃有不同大小的糖果棒:A[i] 是爱丽丝拥有的第 i 块糖的大小,B[j] 是鲍勃拥有的第 j 块糖的大小。
因为他们是朋友,所以他们想交换一个糖果棒,这样交换后,他们都有相同的糖果总量。(一个人拥有的糖果总量是他们拥有的糖果棒大小的总和。)
返回一个整数数组 ans,其中 ans[0] 是爱丽丝必须交换的糖果棒的大小,ans[1] 是 Bob 必须交换的糖果棒的大小。
如果有多个答案,你可以返回其中任何一个。保证答案存在。
示例 1:
输入:A = [1,1], B = [2,2] 输出:[1,2]
示例 2:
输入:A = [1,2], B = [2,3] 输出:[1,2]
示例 3:
输入:A = [2], B = [1,3] 输出:[2,3]
示例 4:
输入:A = [1,2,5], B = [2,4] 输出:[5,4]
提示:
- 1 <= A.length <= 10000
- 1 <= B.length <= 10000
- 1 <= A[i] <= 100000
- 1 <= B[i] <= 100000
- 保证爱丽丝与鲍勃的糖果总量不同。
- 答案肯定存在。
错误的一次原因是没有想到sub也可能为负数,爱丽丝交换前的总量可能比bob大也可能比bob小。
class Solution {
public:
vector<int> fairCandySwap(vector<int>& A, vector<int>& B) {
int sumA = 0;
int sumB = 0;
map<int, int> check;
int len1 = A.size();
int len2 = B.size();
for(int i = 0; i < len1; i++)
{
sumA += A[i];
}
for(int i = 0; i < len2; i++)
{
sumB += B[i];
check[B[i]] = 1;
}
int sub = (sumB - sumA) / 2;
vector<int> ans(2, 0);
for(int i = 0; i < len1; i++)
{
if(check[A[i] + sub] != 0)
{
ans[0] = A[i];
ans[1] = A[i] + sub;
break;
}
}
return ans;
}
};
Leetcode888.Fair Candy Swap公平的糖果交换的更多相关文章
- 【LeetCode】888. Fair Candy Swap 公平的糖果棒交换(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人公众号: 每日算法题 本文关键词:力扣,LeetCode,算法题,算法,Python 目录 题目描述 题目大意 解题方法 代码 刷题心得 关于作 ...
- [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 ...
- 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_easy】888. Fair Candy Swap
problem 888. Fair Candy Swap solution: class Solution { public: vector<int> fairCandySwap(vect ...
- [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 ...
- C#LeetCode刷题之#888-公平的糖果交换(Fair Candy Swap)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3758 访问. 爱丽丝和鲍勃有不同大小的糖果棒:A[i] 是爱丽丝 ...
- LeetCode.888-公平的糖果交换(Fair Candy Swap)
这是悦乐书的第339次更新,第363篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第208题(顺位题号是888).Alice和Bob有不同大小的糖果棒:A[i]是Alic ...
- 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 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 ...
随机推荐
- python登录aspx网站
1.安装模块 2.准备aspx登录页面 3.示例代码 #coding:utf-8 import re from bs4 import BeautifulSoup import gzip import ...
- 19-10-19-I
中午考试困够呛. T1 我想打矩阵快速幂,然后我咕了 T2 打T1了所以又咕了. T3 每一个黑点更新答案只有两种方式: 更新子树. 更新父链上的兄弟,叔伯,…… 于是: 把树拍在$DFS$序上. 更 ...
- XAMPP:继MAMP之后,Mac OS X平台下又一款帮你快速搭建服务器环境软件
之前,有很多人都有打算在自己的Mac OS下搭建一个本地测试用的Apache+PHP+MySQL网络服务器环境. 在目前而言至少有3种办法可以实现这一点: 通过Xcode开发者套件和php.Apach ...
- Miller Rabin算法学习笔记
定义: Miller Rabin算法是一个随机化素数测试算法,作用是判断一个数是否是素数,且只要你脸不黑以及常数不要巨大一般来讲都比\(O(\sqrt n)\)的朴素做法更快. 定理: Miller ...
- NSIS使用WinVer.nsh头文件判断操作系统版本
NSIS使用WinVer.nsh头文件判断操作系统版本,首先请下载最新的WinVer.nsh: http://nsis.sourceforge.net/Include/WinVer.nsh(下载后置于 ...
- OpenCASCADE动画功能
OpenCASCADE动画功能 eryar@163.com 1.Introduction OpenCASCADE提供了类AIS_Animation等来实现简单的动画功能. 从其类图可以看出,动画功能有 ...
- jquery的each()遍历和ajax传值
页面展示 JS代码部分 /*功能:删除选中用户信息数据*/ function delUser(){ $("#delU").click(function(){ var unoStr ...
- 【DM642学习笔记三】flash的烧写
ICETEK-DM642-PCI板上的29L008B芯片提供了8M位的Flash空间(访问地址空间是CE1,90000000h~90080000h).主要用于自启动功能和存储FPGA的配置数据. 一. ...
- 稀疏表示step by step(转)
原文地址:稀疏表示step by step(转)作者:野火春风 稀疏表示step by step(1) 声明:本人属于绝对的新手,刚刚接触“稀疏表示”这个领域.之所以写下以下的若干个连载,是鼓 ...
- jQuery事件绑定的四种方法
jQuery中提供了四种绑定事件的方法,分别是bind.live.delegate.on,对应的解除监听的函数分别是unbind.die.undelegate.off: 一.on()方法(首选方法) ...