leetcode387:字符串中的第一个唯一字符

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

示例:

s = "leetcode"
返回 0

s = "loveleetcode"
返回 2

=============================Python=============================

class Solution:
    def firstUniqChar(self, s: str) -> int:
        from collections import Counter
        cou = Counter(s)
        for ind in range(len(s)):
            if cou[s[ind]] == 1:
                return ind
        return -1

===================================Go===================================

/**
由于字母共有 26 个,所以可以声明一个 26 个长度的数组(该种方法在本类题型很常用)因为字符串中字母可能是重复的,所以可以先进行第一次遍历,在数组中记录每个字母的最后一次出现的所在索引。然后再通过一次循环,比较各个字母第一次出现的索引是否为最后一次的索引。如果是,我们就找到了我们的目标,如果不是我们将其设为 -1(标示该元素非目标元素)如果第二次遍历最终没有找到目标,直接返回 -1即可。*/
func firstUniqChar(s string) int {
    var arr [26]int
    for i, k := range s{
        arr[k - 'a'] = i
    }
    for i,k := range s{
        if i == arr[k - 'a']{
            return i
        } else {
            arr[k - 'a'] = -1
        }
    }
    return -1
}

 

posted @ 2020-08-16 14:56  LinBupt  阅读(125)  评论(0编辑  收藏  举报