LeetCodeDiary

A Diary for solving LeetCode problems

View on GitHub
'''
Description: 
Autor: Au3C2
Date: 2021-02-01 18:37:07
LastEditors: Au3C2
LastEditTime: 2021-02-01 18:43:24
'''
class Solution:
    def fairCandySwap(self, A: List[int], B: List[int]) -> List[int]:
        t = (sum(B) - sum(A))/2
        # suma - a + b = sumb - b + a
        # (sumb-suma+2a)/2 = b
        # (suma-sumb+2b)/2 = a
        ans = {}
        for a in A:
            ans[t+a] = a
        for b in B:
            a = ans.get(b)
            if a:
                return [a,b]

# 数组,简单,每日一题
# https://leetcode-cn.com/problems/fair-candy-swap/