标签:hash 循环 size 步骤 int 思路 highlight 队列 title
如果当前字符流没有存在出现一次的字符,返回#字符。
class Solution { public: //Insert one char from stringstream void Insert(char ch) { ++cnt[ch-‘\0‘]; if(cnt[ch-‘\0‘]==1) data.push(ch); } //return the first appearence once char in current stringstream char FirstAppearingOnce() { while(!data.empty() && cnt[data.front()] >=2 ) data.pop(); if(data.empty()) return ‘#‘; return data.front(); } Solution() { memset(cnt,0,sizeof(cnt)); } private: queue<char> data; unsigned cnt[128]; };
自己想到了哈希表,却不知道如何具体落实!
class Solution { public: //Insert one char from stringstream string s; char hash[256]={0}; void Insert(char ch) { s+=ch; hash[ch]++; } //return the first appearence once char in current stringstream char FirstAppearingOnce() { int size=s.size(); for(int i=0;i<size;i++) { if(hash[s[i]]==1) return s[i]; } return ‘#‘; } };
标签:hash 循环 size 步骤 int 思路 highlight 队列 title
原文地址:http://www.cnblogs.com/dd2hm/p/7447065.html