【剑指Offer】34.第一个只出现一次的字符(Python实现)

题目描述

在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写).

解法一:映射法

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

猜你喜欢

转载自blog.csdn.net/qq_36936730/article/details/104690757