执行用时: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
classSolution{ funcfirstUniqChar(_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 } }