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 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.
题目分析及思路
给定两组整数数组,分别代表两个人所拥有的糖果长度。要求经过一次交换后,两人所拥有的糖果长度相等。可以先将两人要交换的糖果长度分别设为x和y,然后求解。根据得到的结果遍历数组即可。
python代码
class Solution:
def fairCandySwap(self, A: List[int], B: List[int]) -> List[int]:
s_a, s_b = sum(A), sum(B)
set_b = set(B)
for x in A:
if x + (s_b-s_a) // 2 in set_b:
return [x, x+(s_b-s_a) // 2]
LeetCode 888 Fair Candy Swap 解题报告的更多相关文章
- [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(C++)
题目: Alice and Bob have candy bars of different sizes: A[i] is the size of the i-th bar of candy that ...
- 【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】723. Candy Crush 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 日期 题目地址:https://leetcode ...
- LeetCode 2 Add Two Sum 解题报告
LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
随机推荐
- Asp.Net T4模板生成三层架构
1.T4 Editor安装 T4:根据模板生成文件,例如model等 vs中默认t4模板编码是没有提示和高亮的,需使用以下插件,免费的 https://t4-editor.tangible-engin ...
- Spark学习笔记——键值对操作
键值对 RDD是 Spark 中许多操作所需要的常见数据类型 键值对 RDD 通常用来进行聚合计算.我们一般要先通过一些初始 ETL(抽取.转化.装载)操作来将数据转化为键值对形式. Spark 为包 ...
- session一直报错Session store not set on request
Route::group(['middleware' => ['web']], function () { //});仍然报错,看了 session是使用默认file,没问题:app/stora ...
- Hibernate -- Dao层 -- CURD -- 随记
根据Where 参数 查询记录总数 .拼接SQL语句 .获取Session(hibernateTemplate.getSessionFactory().getCurrentSession()),调用C ...
- Android中使用第三方jar包
步骤: 方法1:Eclipse下, 右键工程, Build path, java build path, 选择libraries 在右边的按钮中点击“Add Library” 选择“User libr ...
- 【!Important】如何保证线程执行的先后顺序
1.假设有三个线程,分别为T1.T2.T3,如果让线程T2在线程T1之后执行,在线程T3之前执行. 使用线程的join方法,该方法的作用是“等待线程执行结束”,即join()方法后面的代码块都要等待现 ...
- npm更新升级
更新 npm install npm -g
- 跟bWAPP学WEB安全(PHP代码)--终结篇:文件目录遍历、文件上传、SSRF、CSRF、XXE、文件包含
前言 过年过的很不顺,家里领导和我本人接连生病,年前腊月29才都治好出院,大年初六家里的拉布拉多爱犬又因为细小医治无效离开了,没能过年回家,花了好多钱,狗狗还离世了.所以也就没什么心思更新博客.今天初 ...
- linux命令之间的分号,&&, ||
在用linux命令时候, 我们经常需要同时执行多条命令, 那么命令之间该如何分割呢? 分号: 顺序地独立执行各条命令, 彼此之间不关心是否失败, 所有命令都会执行. && : 顺序执 ...
- day_6.9py网络编程
.路由器:能够链接不同的网络使他们之间能够通信 mac就是手拉手传输数据用的