Skip to content

Commit f5bca31

Browse files
authored
Update README.md
1 parent a9b069d commit f5bca31

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

lcci/17.04.Missing Number/README.md

+16-4
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
```
@@ -217,6 +220,15 @@ var missingNumber = function (nums) {
217220
};
218221
```
219222

223+
```swift
224+
class Solution {
225+
func missingNumber(_ nums: [Int]) -> Int {
226+
let n = nums.count
227+
return n * (n + 1) / 2 - nums.reduce(0, +)
228+
}
229+
}
230+
```
231+
220232
<!-- tabs:end -->
221233

222234
### 方法三:位运算

0 commit comments

Comments
 (0)