题目描述

请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g"。当从该字符流中读出前六个字符“google"时,第一个只出现一次的字符是"l"。

输出描述:

如果当前字符流没有存在出现一次的字符,返回#字符。

时间限制:1秒;空间限制:32768K;本题知识点:字符串

解题思路

用一个足够大的数组来记录对应ascii码值的字符出现的次数,再判断字符流中第一个不重复的字符。

Python代码:

# -*- coding:utf-8 -*-
class Solution:
    # 返回对应char
    def __init__(self):
        self.s = [0]*256 #ascii码表记录出现次数
        self.l='' #记录输入字符流
    def FirstAppearingOnce(self):
        # write code here
        for i in self.l:
            if self.s[ord(i)] == 1: #仅出现一次
                return i
        return '#'
    def Insert(self, char):
        # write code here
        self.l = self.l + char
        self.s[ord(char)] += 1

 

相关文章:

  • 2021-09-18
  • 2022-12-23
  • 2021-07-10
  • 2022-12-23
  • 2021-06-20
  • 2022-01-17
  • 2022-01-06
猜你喜欢
  • 2022-12-23
  • 2022-01-02
  • 2022-01-16
  • 2021-10-04
  • 2022-02-19
  • 2022-12-23
  • 2021-11-26
相关资源
相似解决方案