From de772990a4d0064fdb18054050d284b4b574ef13 Mon Sep 17 00:00:00 2001 From: Lanre Adedara Date: Wed, 8 May 2024 08:05:09 +0100 Subject: [PATCH 1/2] Swift Implementation for LCCI 16.21 --- lcci/16.21.Sum Swap/README.md | 31 ++++++++++++++++++++++++++++++ lcci/16.21.Sum Swap/README_EN.md | 31 ++++++++++++++++++++++++++++++ lcci/16.21.Sum Swap/Solution.swift | 28 +++++++++++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 lcci/16.21.Sum Swap/Solution.swift diff --git a/lcci/16.21.Sum Swap/README.md b/lcci/16.21.Sum Swap/README.md index b70950be66aa2..70cadbd2c9a2f 100644 --- a/lcci/16.21.Sum Swap/README.md +++ b/lcci/16.21.Sum Swap/README.md @@ -148,6 +148,37 @@ function findSwapValues(array1: number[], array2: number[]): number[] { } ``` +```swift +class Solution { + func findSwapValues(_ array1: [Int], _ array2: [Int]) -> [Int] { + var s1 = 0, s2 = 0 + var set = Set() + + for x in array1 { + s1 += x + } + for x in array2 { + s2 += x + set.insert(x) + } + + let diff = s1 - s2 + if diff % 2 != 0 { + return [] + } + let target = diff / 2 + + for a in array1 { + let b = a - target + if set.contains(b) { + return [a, b] + } + } + return [] + } +} +``` + diff --git a/lcci/16.21.Sum Swap/README_EN.md b/lcci/16.21.Sum Swap/README_EN.md index 1e96bc3427299..3879a6c4c21d9 100644 --- a/lcci/16.21.Sum Swap/README_EN.md +++ b/lcci/16.21.Sum Swap/README_EN.md @@ -154,6 +154,37 @@ function findSwapValues(array1: number[], array2: number[]): number[] { } ``` +```swift +class Solution { + func findSwapValues(_ array1: [Int], _ array2: [Int]) -> [Int] { + var s1 = 0, s2 = 0 + var set = Set() + + for x in array1 { + s1 += x + } + for x in array2 { + s2 += x + set.insert(x) + } + + let diff = s1 - s2 + if diff % 2 != 0 { + return [] + } + let target = diff / 2 + + for a in array1 { + let b = a - target + if set.contains(b) { + return [a, b] + } + } + return [] + } +} +``` + diff --git a/lcci/16.21.Sum Swap/Solution.swift b/lcci/16.21.Sum Swap/Solution.swift new file mode 100644 index 0000000000000..7a676b163a5c1 --- /dev/null +++ b/lcci/16.21.Sum Swap/Solution.swift @@ -0,0 +1,28 @@ +class Solution { + func findSwapValues(_ array1: [Int], _ array2: [Int]) -> [Int] { + var s1 = 0, s2 = 0 + var set = Set() + + for x in array1 { + s1 += x + } + for x in array2 { + s2 += x + set.insert(x) + } + + let diff = s1 - s2 + if diff % 2 != 0 { + return [] + } + let target = diff / 2 + + for a in array1 { + let b = a - target + if set.contains(b) { + return [a, b] + } + } + return [] + } +} \ No newline at end of file From d6db16997031ed5e1f1059b1ff43ff20c208659b Mon Sep 17 00:00:00 2001 From: klever34 Date: Wed, 8 May 2024 07:19:18 +0000 Subject: [PATCH 2/2] style: format code and docs with prettier --- lcci/16.21.Sum Swap/README.md | 6 +++--- lcci/16.21.Sum Swap/README_EN.md | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lcci/16.21.Sum Swap/README.md b/lcci/16.21.Sum Swap/README.md index 70cadbd2c9a2f..de33a7887c4d2 100644 --- a/lcci/16.21.Sum Swap/README.md +++ b/lcci/16.21.Sum Swap/README.md @@ -153,7 +153,7 @@ class Solution { func findSwapValues(_ array1: [Int], _ array2: [Int]) -> [Int] { var s1 = 0, s2 = 0 var set = Set() - + for x in array1 { s1 += x } @@ -161,13 +161,13 @@ class Solution { s2 += x set.insert(x) } - + let diff = s1 - s2 if diff % 2 != 0 { return [] } let target = diff / 2 - + for a in array1 { let b = a - target if set.contains(b) { diff --git a/lcci/16.21.Sum Swap/README_EN.md b/lcci/16.21.Sum Swap/README_EN.md index 3879a6c4c21d9..977268ade5a2c 100644 --- a/lcci/16.21.Sum Swap/README_EN.md +++ b/lcci/16.21.Sum Swap/README_EN.md @@ -159,7 +159,7 @@ class Solution { func findSwapValues(_ array1: [Int], _ array2: [Int]) -> [Int] { var s1 = 0, s2 = 0 var set = Set() - + for x in array1 { s1 += x } @@ -167,13 +167,13 @@ class Solution { s2 += x set.insert(x) } - + let diff = s1 - s2 if diff % 2 != 0 { return [] } let target = diff / 2 - + for a in array1 { let b = a - target if set.contains(b) {