题解 | #字符流中第一个不重复的字符#

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

https://www.nowcoder.com/practice/00de97733b8e4f97a3fb5c680ee10720?tpId=265&rp=1&ru=%2Fexam%2Foj%2Fta&qru=%2Fexam%2Foj%2Fta&sourceUrl=%2Fexam%2Foj%2Fta%3FjudgeStatus%3D3%26page%3D1%26pageSize%3D50%26search%3D%26tpId%3D13%26type%3D265&difficulty=&judgeStatus=3&tags=&title=&gioEnter=menu

class Solution
{
  public:
  //Insert one char from stringstream
    void Insert(char ch) {
      str += ch;
      ++hash[ch];
    }
  //return the first appearence once char in current stringstream
    char FirstAppearingOnce() {
      if (str.empty()) {
        return '#';
      }
      
      for (int i = 0; i < str.size(); ++i) {
        if (hash[str[i]] == 1) {
          return str[i];
        }
      }
      
      return '#';
    }
  
  private:
    std::string str;
    std::unordered_map<char, int> hash;
};
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务