Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8c59e5d

Browse files
klever34yanglbme
andauthored
feat: add swift implementation to lcci problem: No.17.04 (doocs#2772)
--------- Co-authored-by: Libin YANG <contact@yanglibin.info>
1 parent 22b7dc4 commit 8c59e5d

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

‎lcci/17.04.Missing Number/README.md‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,20 @@ var missingNumber = function (nums) {
119119
};
120120
```
121121

122+
```swift
123+
class Solution {
124+
func missingNumber(_ nums: [Int]) -> Int {
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
132+
}
133+
}
134+
```
135+
122136
<!-- tabs:end -->
123137

124138
### 方法二:求和
@@ -206,6 +220,15 @@ var missingNumber = function (nums) {
206220
};
207221
```
208222

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+
209232
<!-- tabs:end -->
210233

211234
### 方法三:位运算

‎lcci/17.04.Missing Number/README_EN.md‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,20 @@ var missingNumber = function (nums) {
119119
};
120120
```
121121

122+
```swift
123+
class Solution {
124+
func missingNumber(_ nums: [Int]) -> Int {
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
132+
}
133+
}
134+
```
135+
122136
<!-- tabs:end -->
123137

124138
### Solution 2
@@ -202,6 +216,15 @@ var missingNumber = function (nums) {
202216
};
203217
```
204218

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+
205228
<!-- tabs:end -->
206229

207230
### Solution 3
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
func missingNumber(_ nums: [Int]) -> Int {
3+
let nums = nums.sorted()
4+
for (i, x) in nums.enumerated() {
5+
if i != x {
6+
return i
7+
}
8+
}
9+
return nums.count
10+
}
11+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution {
2+
func missingNumber(_ nums: [Int]) -> Int {
3+
let n = nums.count
4+
return n * (n + 1) / 2 - nums.reduce(0, +)
5+
}
6+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /