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

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

http://www.nowcoder.com/practice/00de97733b8e4f97a3fb5c680ee10720

  • 对哈希表、代码的实现能力均需要提高。
class Solution
{
public:
  //Insert one char from stringstream
    map<char,int> m1;
    char cur = ' ';
    int index = 0;
    
    vector<char> res;
    
    void Insert(char ch) {
         m1[ch]++;
         res.push_back(ch);
        
    }
  //return the first appearence once char in current stringstream
    char FirstAppearingOnce() {
      int i = 0;
      if(m1[res[index]] == 1){
          return res[index];
      }else{
          for(i = index; i < res.size(); i++){
              if(m1[res[i]] == 1){
                  index = i;
                  return res[index];
                  
                  
              }
             
          }
          if(i == res.size()){
              index = i - 1;
              return '#';
          }
          else{return res[index];}
      }
    }

};
全部评论

相关推荐

12-13 20:26
浙江大学 Java
淬月星辉:把浙大的校名加大加粗,把校徽再贴出来,就OK了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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