1. 程式人生 > >【LeetCode】387. First Unique Character in a String 解題報告(Python)

【LeetCode】387. First Unique Character in a String 解題報告(Python)

作者: 負雪明燭
id: fuxuemingzhu
個人部落格: http://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 -1

日期

2018 年 11 月 16 日 —— 又到週五了!