From abc248640fac4d400c8e18376ebe8f8bd066cfaf Mon Sep 17 00:00:00 2001 From: Lanre Adedara Date: Thu, 9 May 2024 07:43:33 +0100 Subject: [PATCH 1/5] Swift Implementation for LCCI 17.04 --- lcci/17.04.Missing Number/README.md | 11 +++++++++++ lcci/17.04.Missing Number/README_EN.md | 11 +++++++++++ lcci/17.04.Missing Number/Solution.swift | 8 ++++++++ 3 files changed, 30 insertions(+) create mode 100644 lcci/17.04.Missing Number/Solution.swift diff --git a/lcci/17.04.Missing Number/README.md b/lcci/17.04.Missing Number/README.md index 77ec170e6d1aa..f25f1e48d222a 100644 --- a/lcci/17.04.Missing Number/README.md +++ b/lcci/17.04.Missing Number/README.md @@ -119,6 +119,17 @@ var missingNumber = function (nums) { }; ``` +```swift +class Solution { + func missingNumber(_ nums: [Int]) -> Int { + let n = nums.count + let expectedSum = n * (n + 1) / 2 + let actualSum = nums.reduce(0, +) + return expectedSum - actualSum + } +} +``` + ### 方法二:求和 diff --git a/lcci/17.04.Missing Number/README_EN.md b/lcci/17.04.Missing Number/README_EN.md index 8d0c402b9388e..378c21fb8f8ae 100644 --- a/lcci/17.04.Missing Number/README_EN.md +++ b/lcci/17.04.Missing Number/README_EN.md @@ -119,6 +119,17 @@ var missingNumber = function (nums) { }; ``` +```swift +class Solution { + func missingNumber(_ nums: [Int]) -> Int { + let n = nums.count + let expectedSum = n * (n + 1) / 2 + let actualSum = nums.reduce(0, +) + return expectedSum - actualSum + } +} +``` + ### Solution 2 diff --git a/lcci/17.04.Missing Number/Solution.swift b/lcci/17.04.Missing Number/Solution.swift new file mode 100644 index 0000000000000..fa8d9fa96d399 --- /dev/null +++ b/lcci/17.04.Missing Number/Solution.swift @@ -0,0 +1,8 @@ +class Solution { + func missingNumber(_ nums: [Int]) -> Int { + let n = nums.count + let expectedSum = n * (n + 1) / 2 + let actualSum = nums.reduce(0, +) + return expectedSum - actualSum + } +} From 09b421b9a3c31a9ec9b4dab60a4a02fdb23bf7a3 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 9 May 2024 16:45:36 +0800 Subject: [PATCH 2/5] Update and rename Solution.swift to Solution2.swift --- lcci/17.04.Missing Number/Solution.swift | 8 -------- lcci/17.04.Missing Number/Solution2.swift | 6 ++++++ 2 files changed, 6 insertions(+), 8 deletions(-) delete mode 100644 lcci/17.04.Missing Number/Solution.swift create mode 100644 lcci/17.04.Missing Number/Solution2.swift diff --git a/lcci/17.04.Missing Number/Solution.swift b/lcci/17.04.Missing Number/Solution.swift deleted file mode 100644 index fa8d9fa96d399..0000000000000 --- a/lcci/17.04.Missing Number/Solution.swift +++ /dev/null @@ -1,8 +0,0 @@ -class Solution { - func missingNumber(_ nums: [Int]) -> Int { - let n = nums.count - let expectedSum = n * (n + 1) / 2 - let actualSum = nums.reduce(0, +) - return expectedSum - actualSum - } -} diff --git a/lcci/17.04.Missing Number/Solution2.swift b/lcci/17.04.Missing Number/Solution2.swift new file mode 100644 index 0000000000000..ec64d3a4b404a --- /dev/null +++ b/lcci/17.04.Missing Number/Solution2.swift @@ -0,0 +1,6 @@ +class Solution { + func missingNumber(_ nums: [Int]) -> Int { + let n = nums.count + return n * (n + 1) / 2 - nums.reduce(0, +) + } +} From a9b069d36ef349cbbffd0a9df16d48e3471c7556 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 9 May 2024 16:46:15 +0800 Subject: [PATCH 3/5] Update README_EN.md --- lcci/17.04.Missing Number/README_EN.md | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/lcci/17.04.Missing Number/README_EN.md b/lcci/17.04.Missing Number/README_EN.md index 378c21fb8f8ae..f0e867e6a31d8 100644 --- a/lcci/17.04.Missing Number/README_EN.md +++ b/lcci/17.04.Missing Number/README_EN.md @@ -122,10 +122,13 @@ var missingNumber = function (nums) { ```swift class Solution { func missingNumber(_ nums: [Int]) -> Int { - let n = nums.count - let expectedSum = n * (n + 1) / 2 - let actualSum = nums.reduce(0, +) - return expectedSum - actualSum + let nums = nums.sorted() + for (i, x) in nums.enumerated() { + if i != x { + return i + } + } + return nums.count } } ``` @@ -213,6 +216,15 @@ var missingNumber = function (nums) { }; ``` +```swift +class Solution { + func missingNumber(_ nums: [Int]) -> Int { + let n = nums.count + return n * (n + 1) / 2 - nums.reduce(0, +) + } +} +``` + ### Solution 3 From f5bca310fd23714f44259dc11c1f8a4bf7a74f8d Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 9 May 2024 16:47:32 +0800 Subject: [PATCH 4/5] Update README.md --- lcci/17.04.Missing Number/README.md | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/lcci/17.04.Missing Number/README.md b/lcci/17.04.Missing Number/README.md index f25f1e48d222a..88ab47978b1a5 100644 --- a/lcci/17.04.Missing Number/README.md +++ b/lcci/17.04.Missing Number/README.md @@ -122,10 +122,13 @@ var missingNumber = function (nums) { ```swift class Solution { func missingNumber(_ nums: [Int]) -> Int { - let n = nums.count - let expectedSum = n * (n + 1) / 2 - let actualSum = nums.reduce(0, +) - return expectedSum - actualSum + let nums = nums.sorted() + for (i, x) in nums.enumerated() { + if i != x { + return i + } + } + return nums.count } } ``` @@ -217,6 +220,15 @@ var missingNumber = function (nums) { }; ``` +```swift +class Solution { + func missingNumber(_ nums: [Int]) -> Int { + let n = nums.count + return n * (n + 1) / 2 - nums.reduce(0, +) + } +} +``` + ### 方法三:位运算 From 4cadb7e5c6f975edc06c70468ae76c0bd6c7d867 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 9 May 2024 16:49:28 +0800 Subject: [PATCH 5/5] Create Solution.swift --- lcci/17.04.Missing Number/Solution.swift | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 lcci/17.04.Missing Number/Solution.swift diff --git a/lcci/17.04.Missing Number/Solution.swift b/lcci/17.04.Missing Number/Solution.swift new file mode 100644 index 0000000000000..46cae2cc9e1f6 --- /dev/null +++ b/lcci/17.04.Missing Number/Solution.swift @@ -0,0 +1,11 @@ +class Solution { + func missingNumber(_ nums: [Int]) -> Int { + let nums = nums.sorted() + for (i, x) in nums.enumerated() { + if i != x { + return i + } + } + return nums.count + } +}