Leetcode888. 公平的糖果交换
1. 问题描述
2. 思路
2.1 思路1
暴力求解
3. 代码
func fairCandySwap(aliceSizes []int, bobSizes []int) []int {
var res []int
aliceSum, bobSum := 0, 0
for i := 0; i < len(aliceSizes); i++ {
aliceSum += aliceSizes[i]
}
for j := 0; j < len(bobSizes); j++ {
bobSum += bobSizes[j]
}
if aliceSum > bobSum {
temp := (aliceSum - bobSum) / 2
for i := 0; i < len(aliceSizes); i++ {
for j := 0; j < len(bobSizes); j++ {
if aliceSizes[i] - bobSizes[j] == temp {
res = []int{aliceSizes[i], bobSizes[j]}
return res
}
}
}
} else {
temp := (bobSum - aliceSum) / 2
for i := 0; i < len(aliceSizes); i++ {
for j := 0; j < len(bobSizes); j++ {
if bobSizes[j] - aliceSizes[i] == temp {
res = []int{aliceSizes[i], bobSizes[j]}
return res
}
}
}
}
return res
}
|