Skip to content

Commit 377f75f

Browse files
authored
feat: add swift implementation to lcof problem: No.50 (#2935)
1 parent 4eaff48 commit 377f75f

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

lcof/面试题50. 第一个只出现一次的字符/README.md

+23
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,29 @@ public class Solution {
193193
}
194194
```
195195

196+
#### Swift
197+
198+
```swift
199+
class Solution {
200+
func firstUniqChar(_ s: String) -> Character {
201+
var count = [Int](repeating: 0, count: 26)
202+
let aAsciiValue = Int(Character("a").asciiValue!)
203+
204+
for char in s {
205+
count[Int(char.asciiValue!) - aAsciiValue] += 1
206+
}
207+
208+
for char in s {
209+
if count[Int(char.asciiValue!) - aAsciiValue] == 1 {
210+
return char
211+
}
212+
}
213+
214+
return " "
215+
}
216+
}
217+
```
218+
196219
<!-- tabs:end -->
197220

198221
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
func firstUniqChar(_ s: String) -> Character {
3+
var count = [Int](repeating: 0, count: 26)
4+
let aAsciiValue = Int(Character("a").asciiValue!)
5+
6+
for char in s {
7+
count[Int(char.asciiValue!) - aAsciiValue] += 1
8+
}
9+
10+
for char in s {
11+
if count[Int(char.asciiValue!) - aAsciiValue] == 1 {
12+
return char
13+
}
14+
}
15+
16+
return " "
17+
}
18+
}

0 commit comments

Comments
 (0)