387. First Unique Character in a String 字符串中的第一个唯一字符
作者: 负雪明烛 id: fuxuemingzhu 个人博客: https://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/first-unique-character-in-a-string/description/
Given a string, find the first non-repeating character in it and return it’s index. If it doesn’t exist, return -1.
Examples:
s = "leetcode"return 0.
s = "loveleetcode",return 2.Note: You may assume the string contain only lowercase letters.
找出字符串中第一个只出现了1次的字符。
首先做个字符出现次数的统计,然后再次遍历,找出只出现了一次的第一个字符。
class Solution: def firstUniqChar(self, s): """ :type s: str :rtype: int """ count = collections.Counter(s) for i, c in enumerate(s): if count[c] == 1: return i return -12018 年 11 月 16 日 —— 又到周五了!

评论与交流