第一个只出现一次的字符
时间限制:1秒 空间限制:32768K 热度指数:245478
本题知识点: 字符串
算法知识视频讲解
题目描述
在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写).
代码
使用dict
# -*- coding:utf-8 -*-
class Solution:
def FirstNotRepeatingChar(self, s):
# write code here
dict = {}
for i in s:
dict[i] = dict.get(i, 0)+1
for j in range(len(s)):
if dict[s[j]] == 1:
return j
return -1