剑指offer_字符串_第一个只出现一次的字符

字符串_第一个只出现一次的字符

在这里插入图片描述
法一

# -*- coding:utf-8 -*-
class Solution:
    def FirstNotRepeatingChar(self, s):
        # write code here
        res = -1
        for i in range(len(s)):
            if s[i] not in s[:i] and s[i] not in s[i+1:]:
                res = i
                return res
        return res

法二:

# -*- coding:utf-8 -*-
class Solution:
    def FirstNotRepeatingChar(self, s):
        # write code here
        Dict = {}
        res = -1
        for e in s:
            if e in Dict:
                Dict[e] += 1
            else:
                Dict[e] = 1
        for i in range(len(s)):
            if Dict[s[i]] == 1:
                res = i
                return res
        return res
发布了31 篇原创文章 · 获赞 0 · 访问量 747

猜你喜欢

转载自blog.csdn.net/freedomUSTB/article/details/104930513