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

题目描述

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

用例:

输入:
google
对应输出应该为:
4

代码

# -*- coding:utf-8 -*-
class Solution:
    def FirstNotRepeatingChar(self, s):
        # write code here
        if s == '':
            return -1
        else:
            a = list(s)
            res = []
            for i in a:
                count = 0
                for j in a:
                    if i == j:
                        count +=1
                res.append(count)
            return res.index(1)
        
发布了99 篇原创文章 · 获赞 6 · 访问量 3962

猜你喜欢

转载自blog.csdn.net/weixin_42247922/article/details/104012497
今日推荐