class Solution {
public int[] fairCandySwap(int[] A, int[] B) {
//The mathmatics way we have is:
// y = x + (SumB - SumA) / 2
int sumA = 0, sumB = 0;
for(int x: A) sumA += x;
for(int x: B) sumB += x;
int delta = (sumB - sumA) / 2;
Set<Integer> set = new HashSet();
for(int x: B) set.add(x);
for(int x: A) {
if(set.contains( x + delta )) return new int[] {x, x + delta};
}
throw null;
}
}
888. Fair Candy Swap
最新推荐文章于 2019-12-29 17:15:17 发布