-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquestion_3.py
More file actions
65 lines (56 loc) · 1.32 KB
/
question_3.py
File metadata and controls
65 lines (56 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python
# @Time : 2018/5/3 下午9:54
# @Author : cancan
# @File : question_3.py
# @Function : 字符串中的第一个唯一字符
"""
Question:
给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。
Example:
s = "leetcode"
返回 0.
s = "loveleetcode",
返回 2.
Note:
您可以假定该字符串只包含小写字母。
"""
class Solution1:
def firstUniqChar(self, s):
"""
:type s: str
:rtype: int
"""
for i, v in enumerate(s):
if v not in s[i+1:] + s[:i]:
return i
else:
return -1
class Solution2:
def firstUniqChar(self, s):
"""
:type s: str
:rtype: int
"""
t = []
for i, v in enumerate(s):
if v in t:
continue
if v in s[i+1:]:
t.append(v)
else:
return i
else:
return -1
class Solution3:
def firstUniqChar(self, s):
"""
:type s: str
:rtype: int
"""
l = list(set(s))
i = [s.index(t) for t in l if s.count(t) == 1]
return min(i) if i else -1
if __name__ == "__main__":
a = Solution3()
s = "leetcode"
print(a.firstUniqChar(s))