算法16-----字符串中的第一个唯一字符

1、题目

给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。

案例:

s = "leetcode"
返回 0.

s = "loveleetcode",
返回 2。

2、代码:第一个出现相同的就消掉一样的,用replace。

 

def firstUniqChar(self, s): unique = s while unique: if unique[0] in unique[1::]: unique = unique.replace(unique[0], "") else: return s.find(unique[0]) return -1



猜你喜欢

转载自www.cnblogs.com/Lee-yl/p/9020645.html