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

 




相关文章:

  • 2021-06-26
  • 2021-06-30
  • 2021-11-17
  • 2022-12-23
  • 2022-12-23
  • 2021-08-09
  • 2022-01-11
  • 2022-12-23
猜你喜欢
  • 2021-09-01
  • 2022-03-06
  • 2021-09-27
  • 2022-12-23
  • 2021-08-08
  • 2022-12-23
  • 2022-01-28
相关资源
相似解决方案