前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >剑指offer 字符流中第一个不重复的字符

剑指offer 字符流中第一个不重复的字符

作者头像
week
发布2018-12-25 11:38:26
3240
发布2018-12-25 11:38:26
举报
文章被收录于专栏:用户画像用户画像

题目描述

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

输出描述:

代码语言:javascript
复制
如果当前字符流没有存在出现一次的字符,返回#字符。
代码语言:javascript
复制
package offer.FirstAppearingOnce;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Solution {
    public Map map=new HashMap();
    public List list=new ArrayList();
    //Insert one char from stringstream
    public void Insert(char ch)
    {
        Integer cnt= (Integer) map.get(ch);
        if(cnt==null){
            map.put(ch,1);
        }else{
            map.put(ch,cnt+1);
        }
        list.add(ch);
    }
    //return the first appearence once char in current stringstream
    public char FirstAppearingOnce()
    {
        char rs='#';
        int size=list.size();
        for(int i=0;i<size;i++){
            char ch= (char) list.get(i);
            if(1 == (Integer) map.get(ch)){
                rs=ch;
                break;
            }
        }
        return rs;
    }

    public static void main(String[] args) {
        Solution solution=new Solution();
        String s="helloworld";
        int len=s.length();
        for(int i=0;i<len;i++){
            char ch=s.charAt(i);
            solution.Insert(ch);
        }
        char rs=solution.FirstAppearingOnce();
        System.out.print(rs);
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年12月11日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目描述
  • 输出描述:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档