zoukankan      html  css  js  c++  java
  • 52、字符流中第一个不重复的字符

    一、题目

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

    二、解法

     1 mport java.util.ArrayList;
     2 import java.util.HashMap;
     3 public class Solution {
     4  HashMap<Character,Integer> map = new HashMap();
     5     ArrayList<Character> list = new ArrayList<Character>();
     6     //insert one char from stringstream
     7     public void Insert(char ch){
     8         if(map.containsKey(ch)){
     9             map.put(ch, map.get(ch)+1);
    10         }else{
    11             map.put(ch, 1);
    12         }
    13         list.add(ch);
    14     }
    15      public char FirstAppearingOnce()
    16       {
    17         char c = '#';
    18         for(char key : list){
    19             if(map.get(key) == 1){
    20                 c = key;
    21                 break;
    22             }
    23         }
    24         return c;
    25       }
    26 }
  • 相关阅读:
    tomcat监控与优化
    rpm打包
    Rewrite和location 区别
    LNMP服务
    yum仓库脚本
    用户管理的脚本2
    pxe装机脚本
    用户管理的脚本1
    磁盘管理综合测试题
    MySQL 增量备份介绍及案例演示
  • 原文地址:https://www.cnblogs.com/fankongkong/p/7460233.html
Copyright © 2011-2022 走看看