★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-xtgkricu-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★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.git
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.)github
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:spa
Input: A = [1,1], B = [2,2] Output: [1,2]
Example 2:code
Input: A = [1,2], B = [2,3] Output: [1,2]
Example 3:htm
Input: A = [2], B = [1,3] Output: [2,3]
Example 4:blog
Input: A = [1,2,5], B = [2,4] Output: [5,4]
Note:ip
1 <= A.length <= 10000
1 <= B.length <= 10000
1 <= A[i] <= 100000
1 <= B[i] <= 100000
愛麗絲和鮑勃有不一樣大小的糖果棒: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
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 }