Skip to content

Commit 006523f

Browse files
authored
feat: add swift solution to lc problem: No.0137 (#892)
No.0137.Single Number II
1 parent daf06dd commit 006523f

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

Diff for: solution/0100-0199/0137.Single Number II/README.md

+20
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,26 @@ public:
122122
};
123123
```
124124
125+
### **Swift**
126+
127+
```swift
128+
class Solution {
129+
func singleNumber(_ nums: [Int]) -> Int {
130+
var a = nums.sorted()
131+
var n = a.count
132+
for i in stride(from: 0, through: n - 1, by: 3) {
133+
if i == n - 1 {
134+
return a[i]
135+
}
136+
else if a[i] != a[i + 1] {
137+
return a[i]
138+
}
139+
}
140+
return 0
141+
}
142+
}
143+
```
144+
125145
### **...**
126146

127147
```

Diff for: solution/0100-0199/0137.Single Number II/README_EN.md

+20
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,26 @@ public:
101101
};
102102
```
103103
104+
### **Swift**
105+
106+
```swift
107+
class Solution {
108+
func singleNumber(_ nums: [Int]) -> Int {
109+
var a = nums.sorted()
110+
var n = a.count
111+
for i in stride(from: 0, through: n - 1, by: 3) {
112+
if i == n - 1 {
113+
return a[i]
114+
}
115+
else if a[i] != a[i + 1] {
116+
return a[i]
117+
}
118+
}
119+
return 0
120+
}
121+
}
122+
```
123+
104124
### **...**
105125

106126
```
+15
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: 3) {
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)