美文网首页
面试题50:第一个只出现一次的字符

面试题50:第一个只出现一次的字符

作者: 小歪与大白兔 | 来源:发表于2018-08-22 21:35 被阅读0次

题目描述:

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

解题思路:

# -*- coding:utf-8 -*-
class Solution:
    def FirstNotRepeatingChar(self, s):
        # write code here
        if len(s)<=0:
            return -1
        hash_list = [0]*256
        for str in s:
            hash_list[ord(str)] += 1
        for i in range(len(s)):
            if hash_list[ord(s[i])] == 1:
                return i
        

相关文章

网友评论

      本文标题:面试题50:第一个只出现一次的字符

      本文链接:https://www.haomeiwen.com/subject/jhluiftx.html