,敢教日月换新天。为有牺牲多壮志

[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 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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 }
复制代码

Runtime: 516 ms
Memory Usage: 19.1 MB
复制代码
 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 }
复制代码

 

posted @   为敢技术  阅读(239)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示
哥伦布
09:09发布
哥伦布
09:09发布
3°
多云
东南风
3级
空气质量
相对湿度
47%
今天
中雨
3°/15°
周三
中雨
3°/13°
周四
小雪
-1°/6°