Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit abc2486

Browse files
committedMay 9, 2024··
Swift Implementation for LCCI 17.04
1 parent bde04f3 commit abc2486

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed
 

‎lcci/17.04.Missing Number/README.md

+11
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,17 @@ var missingNumber = function (nums) {
119119
};
120120
```
121121

122+
```swift
123+
class Solution {
124+
func missingNumber(_ nums: [Int]) -> Int {
125+
let n = nums.count
126+
let expectedSum = n * (n + 1) / 2
127+
let actualSum = nums.reduce(0, +)
128+
return expectedSum - actualSum
129+
}
130+
}
131+
```
132+
122133
<!-- tabs:end -->
123134

124135
### 方法二:求和

‎lcci/17.04.Missing Number/README_EN.md

+11
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,17 @@ var missingNumber = function (nums) {
119119
};
120120
```
121121

122+
```swift
123+
class Solution {
124+
func missingNumber(_ nums: [Int]) -> Int {
125+
let n = nums.count
126+
let expectedSum = n * (n + 1) / 2
127+
let actualSum = nums.reduce(0, +)
128+
return expectedSum - actualSum
129+
}
130+
}
131+
```
132+
122133
<!-- tabs:end -->
123134

124135
### Solution 2
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution {
2+
func missingNumber(_ nums: [Int]) -> Int {
3+
let n = nums.count
4+
let expectedSum = n * (n + 1) / 2
5+
let actualSum = nums.reduce(0, +)
6+
return expectedSum - actualSum
7+
}
8+
}

0 commit comments

Comments
 (0)
Please sign in to comment.