387. 字符串中的第一个唯一字符

题目链接:https://leetcode-cn.com/problems/first-unique-character-in-a-string/

执行用时:100 ms, 在所有 Swift 提交中击败了90.08%的用户
内存消耗:13.9 MB, 在所有 Swift 提交中击败了90.08%的用户

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
func firstUniqChar(_ s: String) -> Int {
var res = 0
var table = [Int].init(repeating: 0, count: 26)
let chValue = Int("a".unicodeScalars.first!.value)
for c in s.unicodeScalars {
table[Int(c.value) - chValue] += 1
}
for i in s.unicodeScalars {
if table[Int(i.value) - chValue] == 1 {
return res
}
res += 1
}
return -1
}
}

387. 字符串中的第一个唯一字符
https://pisces34.github.io/2021/09/26/leetcode/387/
发布于
2021年9月26日
许可协议