Skip to content

Commit 4efc3e3

Browse files
authored
feat: add swift solution to lc problem: No.0136 (#891)
No.0136.Single Number
1 parent a088770 commit 4efc3e3

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

solution/0100-0199/0136.Single Number/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,26 @@ impl Solution {
152152
}
153153
```
154154

155+
### **Swift**
156+
157+
```swift
158+
class Solution {
159+
func singleNumber(_ nums: [Int]) -> Int {
160+
var a = nums.sorted()
161+
var n = a.count
162+
for i in stride(from: 0, through: n - 1, by: 2) {
163+
if i == n - 1 {
164+
return a[i]
165+
}
166+
else if a[i] != a[i + 1] {
167+
return a[i]
168+
}
169+
}
170+
return 0
171+
}
172+
}
173+
```
174+
155175
### **...**
156176

157177
```

solution/0100-0199/0136.Single Number/README_EN.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,26 @@ impl Solution {
116116
}
117117
```
118118

119+
### **Swift**
120+
121+
```swift
122+
class Solution {
123+
func singleNumber(_ nums: [Int]) -> Int {
124+
var a = nums.sorted()
125+
var n = a.count
126+
for i in stride(from: 0, through: n - 1, by: 2) {
127+
if i == n - 1 {
128+
return a[i]
129+
}
130+
else if a[i] != a[i + 1] {
131+
return a[i]
132+
}
133+
}
134+
return 0
135+
}
136+
}
137+
```
138+
119139
### **...**
120140

121141
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
func singleNumber(_ nums: [Int]) -> Int {
3+
var a = nums.sorted()
4+
var n = a.count
5+
for i in stride(from: 0, through: n - 1, by: 2) {
6+
if i == n - 1 {
7+
return a[i]
8+
}
9+
else if a[i] != a[i + 1] {
10+
return a[i]
11+
}
12+
}
13+
return 0
14+
}
15+
}

0 commit comments

Comments
 (0)