Skip to content

Commit a9b069d

Browse files
authored
Update README_EN.md
1 parent 09b421b commit a9b069d

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

lcci/17.04.Missing Number/README_EN.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,13 @@ var missingNumber = function (nums) {
122122
```swift
123123
class Solution {
124124
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
125+
let nums = nums.sorted()
126+
for (i, x) in nums.enumerated() {
127+
if i != x {
128+
return i
129+
}
130+
}
131+
return nums.count
129132
}
130133
}
131134
```
@@ -213,6 +216,15 @@ var missingNumber = function (nums) {
213216
};
214217
```
215218

219+
```swift
220+
class Solution {
221+
func missingNumber(_ nums: [Int]) -> Int {
222+
let n = nums.count
223+
return n * (n + 1) / 2 - nums.reduce(0, +)
224+
}
225+
}
226+
```
227+
216228
<!-- tabs:end -->
217229

218230
### Solution 3

0 commit comments

Comments
 (0)