zoukankan      html  css  js  c++  java
  • 剑指OFFER 字符流中第一个不重复的字符

    字符流中第一个不重复的字符

    首先要看懂题目,Insert函数的作用是在字符流中插入一个字符的底层操作,字符流每插入一个字符都会调用一次这个函数,FirstAppearingOnce函数是在字符流中每插入一次字符都会检验一次第一个只出现一次的字符,也就是说,它和前一个函数Insert一样每插入一个字符也会调用一次

    下面的实现思路是,用map来记录字符出现的次数,用链表来记录第一个只出现一次的字符,他们相互协作实时保证list中的第一个元素始终表示第一个只出现一次的字符.

    class Solution
    {
    public:
        map<int,int> m;
        list<int> li;
      //Insert one char from stringstream
        void Insert(char ch)
        {
            if(m[ch] > 0)
            {
                m[ch] = 0;
                li.remove(ch);
            }else{
                m[ch]++;
                li.push_back(ch);
            }
        }
      //return the first appearence once char in current stringstream
        char FirstAppearingOnce()
        {
            if(li.size()==0)return '#';
            return *(li.begin());
        }
    };
    
  • 相关阅读:
    c#剪切板操作
    eclipse mvn build error tips
    Redis Tips
    IntilliJ Idea 使用中的问题与解决方案
    mongo
    python
    SQL Relative
    sybase update
    run current vim file
    git
  • 原文地址:https://www.cnblogs.com/virgildevil/p/12196054.html
Copyright © 2011-2022 走看看