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 ...
随机推荐
- javascript 的原始数据类型
javascript 的原始数据类型有五种,分别是Number.String.Boolean.null.undefined 原始类型数据的特点:值保存在变量本地,且赋值给其他变量后,其它变量改变 ...
- vue.js_05_vue.js的过滤器
1.过滤器的定义和使用 实现:将页面的中的单纯替换成,用户传来的文字. 全局过滤器:所有的Vue对象都可以使用 <body> <div id="app"> ...
- (转)AngularJS中使用的表单验证
原文 http://www.cnblogs.com/woshinidezhu/p/Form-validation-with-AngularJS.html 客户端表单验证是AngularJS里面最酷的 ...
- Make the Most (Hackerrank Codeagon)
题目链接 Problem Statement Codenation is sending N of its employees to a High Profile Business Conferenc ...
- PAT甲级——A1042 Shuffling Machine
Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffling techn ...
- Java的一些小知识
Switch和If的区别: Switch case成功后将不再向下继续执行,而If却要每个条件判断一遍.过于浪费: Java中的多态: 方法重载: 通常是指在同一个类中,相同的方法名对应着不同的方法实 ...
- Django-rest Framework(三)
今天看了drf的五个组件的源码,可读性还是很高的,只是读组件的时候要注意的是 大部分的组件都是由dispatch分发出去的,所以看源码的时候一定要抓住dispatch这条主线,一步一步看下去 一. d ...
- hbase字典顺序存储
rowkey rowkey是行的主键,而且hbase只能用个rowkey,或者一个rowkey范围即scan来查找数据.所以 rowkey的设计是至关重要的,关系到你应用层的查询效率.我们知 ...
- 粗浅看 Tomcat系统架构分析
原文出处: 吴士龙 http://www.importnew.com/21112.html Tomcat的结构很复杂,但是Tomcat也非常的模块化,找到了Tomcat最核心的模块,就抓住了Tomca ...
- LA3695 Distant Galaxy
Distant Galaxy https://vjudge.net/problem/UVALive-3695 You are observing a distant galaxy using a te ...