-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path387.字符串中的第一个唯一字符.py
66 lines (64 loc) · 1.05 KB
/
387.字符串中的第一个唯一字符.py
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
#
# @lc app=leetcode.cn id=387 lang=python3
#
# [387] 字符串中的第一个唯一字符
#
# https://leetcode.cn/problems/first-unique-character-in-a-string/description/
#
# algorithms
# Easy (54.84%)
# Likes: 552
# Dislikes: 0
# Total Accepted: 299.2K
# Total Submissions: 545.1K
# Testcase Example: '"leetcode"'
#
# 给定一个字符串 s ,找到 它的第一个不重复的字符,并返回它的索引 。如果不存在,则返回 -1 。
#
#
#
# 示例 1:
#
#
# 输入: s = "leetcode"
# 输出: 0
#
#
# 示例 2:
#
#
# 输入: s = "loveleetcode"
# 输出: 2
#
#
# 示例 3:
#
#
# 输入: s = "aabb"
# 输出: -1
#
#
#
#
# 提示:
#
#
# 1 <= s.length <= 10^5
# s 只包含小写字母
#
#
#
# @lc code=start
class Solution:
def firstUniqChar(self, s: str) -> int:
map = {}
for i in s:
if i in map:
map[i] +=1
else:
map[i] = 1
for i in map.keys():
if map[i] ==1:
return s.index(i)
return -1
# @lc code=end