剑指offer面试题【50】----第一个只出现一次的字符【python】【enumerate()】

    xiaoxiao2022-07-13  144

    题目描述

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

    代码实现

    import sys class Solution: def FirstNotRepeatingChar(self, s): # write code here if not s: return -1 for i,ch in enumerate(s):#enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。 if s.count(ch)==1: return i

     

    最新回复(0)