在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ Alice and Bob have candy bars of different sizes: 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 If there are multiple answers, you may return any one of them. It is guaranteed an answer exists. Example 1: Input: A = [2,2]
Output: [1,2]
Example 2: Input: A = [2,3]
Output: [1,2]
Example 3: Input: A = [1,3]
Output: [2,3]
Example 4: Input: A = [2,4]
Output: [5,4]
Note:
爱丽丝和鲍勃有不同大小的糖果棒: 因为他们是朋友,所以他们想交换一个糖果棒,这样交换后,他们都有相同的糖果总量。(一个人拥有的糖果总量是他们拥有的糖果棒大小的总和。) 返回一个整数数组 如果有多个答案,你可以返回其中任何一个。保证答案存在。 示例 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] 提示:
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 }
|
请发表评论