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
}
本文介绍了如何解决LeetCode题目888——公平的糖果交换,通过暴力搜索的方法,寻找两个数组中可以进行等价交换的糖果,使得双方拥有相等的糖果数量。详细解释了算法思路和提供了完整的Go语言实现代码。
262

被折叠的 条评论
为什么被折叠?



