[Swift]LeetCode888. 公平的糖果交换 | Fair Candy Swap
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10604113.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
Alice and Bob have candy bars of different sizes: A[i]
is the size of the i
-th bar of candy that Alice has, and B[j]
is the size of the j
-th bar of candy that Bob has.
Since they are friends, they would like to exchange one candy bar each so that after the exchange, they both have the same total amount of candy. (The total amount of candy a person has is the sum of the sizes of candy bars they have.)
Return an integer array ans
where ans[0]
is the size of the candy bar that Alice must exchange, and ans[1]
is the size of the candy bar that Bob must exchange.
If there are multiple answers, you may return any one of them. It is guaranteed an answer exists.
Example 1:
Input: A = [1,1], B = [2,2]
Output: [1,2]
Example 2:
Input: A = [1,2], B = [2,3]
Output: [1,2]
Example 3:
Input: A = [2], B = [1,3]
Output: [2,3]
Example 4:
Input: A = [1,2,5], B = [2,4]
Output: [5,4]
Note:
1 <= A.length <= 10000
1 <= B.length <= 10000
1 <= A[i] <= 100000
1 <= B[i] <= 100000
- It is guaranteed that Alice and Bob have different total amounts of candy.
- It is guaranteed there exists an answer.
爱丽丝和鲍勃有不同大小的糖果棒:A[i]
是爱丽丝拥有的第 i
块糖的大小,B[j]
是鲍勃拥有的第 j
块糖的大小。
因为他们是朋友,所以他们想交换一个糖果棒,这样交换后,他们都有相同的糖果总量。(一个人拥有的糖果总量是他们拥有的糖果棒大小的总和。)
返回一个整数数组 ans
,其中 ans[0]
是爱丽丝必须交换的糖果棒的大小,ans[1]
是 Bob 必须交换的糖果棒的大小。
如果有多个答案,你可以返回其中任何一个。保证答案存在。
示例 1:
输入:A = [1,1], B = [2,2] 输出:[1,2]
示例 2:
输入:A = [1,2], B = [2,3] 输出:[1,2]
示例 3:
输入:A = [2], B = [1,3] 输出:[2,3]
示例 4:
输入:A = [1,2,5], B = [2,4] 输出:[5,4]
提示:
1 <= A.length <= 10000
1 <= B.length <= 10000
1 <= A[i] <= 100000
1 <= B[i] <= 100000
- 保证爱丽丝与鲍勃的糖果总量不同。
- 答案肯定存在。
500ms
1 class Solution { 2 func fairCandySwap(_ A: [Int], _ B: [Int]) -> [Int] { 3 var diff = 0 4 var i = 0 5 var j = 0 6 while i < A.count || j < B.count { 7 if i < A.count { 8 diff -= A[i] 9 i += 1 10 } 11 12 if j < B.count { 13 diff += B[j] 14 j += 1 15 } 16 } 17 18 i = 0 19 j = 0 20 diff /= 2 21 var bSet = Set<Int>() 22 for b in B { 23 bSet.insert(b) 24 } 25 26 for a in A { 27 let pairedValue = a + diff 28 if bSet.contains(pairedValue) { 29 return [a, pairedValue] 30 } 31 } 32 33 return [] 34 } 35 }
504ms
1 class Solution { 2 func fairCandySwap(_ A: [Int], _ B: [Int]) -> [Int] { 3 let aSum = A.reduce(0) { $0 + $1 } 4 let bSum = B.reduce(0) { $0 + $1 } 5 let difference = bSum - aSum 6 let bSet = Set(B.map { $0 }) 7 for a in A { 8 if bSet.contains(a + difference / 2) { 9 return [a, a + difference / 2] 10 } 11 } 12 return [-1, -1] 13 } 14 }
1 class Solution { 2 func fairCandySwap(_ A: [Int], _ B: [Int]) -> [Int] { 3 var dif:Int = (A.reduce(0,+) - B.reduce(0,+)) / 2 4 var S:Set<Int> = Set<Int>() 5 for a in A 6 { 7 S.insert(a) 8 } 9 for b in B 10 { 11 if S.contains(b + dif) 12 { 13 return [b + dif, b] 14 } 15 } 16 return [] 17 } 18 }
552ms
1 class Solution { 2 func fairCandySwap(_ A: [Int], _ B: [Int]) -> [Int] { 3 let sumA = A.reduce(0, +) 4 let sumB = B.reduce(0, +) 5 6 let setA = Set(A) 7 let setB = Set(B) 8 9 for item in setA{ 10 let itemB = (sumB - sumA + 2 * item) / 2 11 if setB.contains(itemB){ 12 return [item, itemB] 13 } 14 } 15 16 return [0,0] 17 } 18 }
556ms
1 class Solution { 2 func fairCandySwap(_ A: [Int], _ B: [Int]) -> [Int] { 3 let sumA = A.reduce(0, +) 4 let sumB = B.reduce(0, +) 5 let bSet = Set(B) 6 for x in A { 7 let y = x + (sumB - sumA) / 2 8 if bSet.contains(y){ 9 return [x,y] 10 } 11 } 12 return [] 13 } 14 }
580ms
1 class Solution { 2 func fairCandySwap(_ A: [Int], _ B: [Int]) -> [Int] { 3 var totalA = 0 4 var totalB = 0 5 var mark = [Int : Bool]() 6 for a in A { 7 totalA += a 8 mark[a] = true 9 } 10 for b in B { 11 totalB += b 12 } 13 14 let dis = (totalB - totalA) / 2 15 for b in B { 16 if let _ = mark[b - dis] { 17 return [b - dis, b] 18 } 19 } 20 return [Int]() 21 } 22 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了