LeetCode-字符串中的第一个唯一字符
题目描述
给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。
示例一:
说明:
思路说明
使用一个列表记录第一次出现的字母,用一个列表重复出现的字母,一个字典记录字母的下标。详见代码。欢迎大家批评指正。
代码实现
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21class Solution(object): def firstUniqChar(self, s): """ :type s: str :rtype: int """ s_list = [] mul_list = [] s_index_dic = {} for i in range(len(s)): if s[i] in s_list: s_list.remove(s[i]) mul_list.append(s[i]) elif s[i] not in mul_list: s_list.append(s[i]) s_index_dic[s[i]] = i if s_list: return s_index_dic[s_list[0]] else: return -1
题目来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/first-unique-character-in-a-string/
最后
以上就是受伤巨人最近收集整理的关于python实现字符串中的第一个唯一字符的全部内容,更多相关python实现字符串中内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复