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 ...
随机推荐
- 【XMPP】Smack源码之初步认识
Smack 概述 Smack是一个用于和XMPP服务器通信的类库,由此可以实现即时通讯和聊天. Smack主要优势 非常简单易用,并且有十分强大的 API.只需三行代码就可以向用户发关文本消息: XM ...
- Python3用gevent写个文件字符串查找器
[本文出自天外归云的博客园] 1.递归遍历目录下所有文件并通过finder函数定位指定格式字符串 2.用来查找字符串的finder函数是自己定义的,这里定义了一个ip_port_finder通过正则表 ...
- pyenv BUILD FAILED解决方法
在本机mac上安装pyenv安装成功后,用pyenv来安装python 3.5.0又出现了如下的问题: -> pyenv install 3.5.0 Downloading Python-3.5 ...
- hdoj:2023
#include <iostream> #include <string> #include <vector> ][],b[],c[]; using namespa ...
- Java知多少(58)线程Runnable接口和Thread类详解
大多数情况,通过实例化一个Thread对象来创建一个线程.Java定义了两种方式: 实现Runnable 接口: 可以继承Thread类. 下面的两小节依次介绍了每一种方式. 实现Runnable接口 ...
- 内存溢出OutOfMemory
https://blog.csdn.net/hzy38324/article/details/76719105 https://blog.csdn.net/u010833547/article/det ...
- tcp 三次握手 转
转载 记得刚毕业找工作面试的时候,经常会被问到:你知道“3次握手,4次挥手”吗?这时候我会“胸有成竹”地“背诵”前期准备好的“答案”,第一次怎么怎么,第二次……答完就没有下文了,面试官貌似也没有深入下 ...
- 面试之C语言字符串操作总结大全(转载)
趁着十一就好好补补数据结构吧,通信这个不软不硬的专业,现在还是得好好学学补习补习,,你这个非211的本科生!虽然拿到了一个offer,但是觉得时间还有,得继续拼一拼,希望不辜负! 1)字符串操作 st ...
- Ubuntu:双(多)网卡绑定(bonding)配置
step 0:安装网卡绑定的功能 apt-get install ifenslave step 1:加载内核模块:编辑 /etc/modules,添加: bonding step 2:编辑网卡配 ...
- python concurrent.futures.Threadpoolexcutor的有界队列和无界队列
1.默认是无界队列,如果生产任务的速度大大超过消费的速度,则会把生产任务无限添加到无界队列中,这样一来控制不了生产速度,二来是会造成系统内存会被队列中的元素堆积增多而耗尽. 2.改写为有界队列 cla ...