leetcode:888. 公平的糖果交换(数组)

题目:

在这里插入图片描述

官方题解python代码:

class Solution:
    def fairCandySwap(self, A: List[int], B: List[int]) -> List[int]:
             sa,sb=sum(A),sum(B)
             setB=set(B)
             for x in A:
                      if x+(sb-sa)/2 in setB:
                               return [x,x+x+(sb-sa)/2]

1.set(列表),把集合直接转化为列表
2.sum(列表),对列表中的内容进行求和。

发布了339 篇原创文章 · 获赞 238 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_42721412/article/details/105567093