字符流中第一个不重复的字符
2015-09-11 11:11
134 查看
请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g"。当从该字符流中读出前六个字符“google"时,第一个只出现一次的字符是"l"。如果当前字符流没有存在出现一次的字符,返回#字符。
import java.util.ArrayList;
import java.util.HashMap;
public class Solution {
private ArrayList<Character> strList=new ArrayList<Character>();
private HashMap<Character,Integer> map=new HashMap<Character,Integer>();
//Insert one char from stringstream
public void Insert(char ch)
{
strList.add(ch);
if(map.get(ch)==null){
map.put(ch,1);
}else{
map.put(ch,map.get(ch)+1);
}
}
//return the first appearence once char in current stringstream
public char FirstAppearingOnce()
{
int n=strList.size();
for(int i=0;i<n;i++){
char ch=strList.get(i);
if(map.get(ch)==1){
return ch;
}
}
return '#';
}
}
import java.util.ArrayList;
import java.util.HashMap;
public class Solution {
private ArrayList<Character> strList=new ArrayList<Character>();
private HashMap<Character,Integer> map=new HashMap<Character,Integer>();
//Insert one char from stringstream
public void Insert(char ch)
{
strList.add(ch);
if(map.get(ch)==null){
map.put(ch,1);
}else{
map.put(ch,map.get(ch)+1);
}
}
//return the first appearence once char in current stringstream
public char FirstAppearingOnce()
{
int n=strList.size();
for(int i=0;i<n;i++){
char ch=strList.get(i);
if(map.get(ch)==1){
return ch;
}
}
return '#';
}
}
相关文章推荐
- 【Python之旅】第二篇(三):基于列表处理的购物清单程序
- heat项目动态(2015.9.11)
- 去掉android中的标题显示
- 远程桌面无法使用剪切板的有效解决方法
- 25个Java机器学习的工具与库(英)
- cf 577B
- 作为程序员你必须知道的几点
- java并发基础
- MySQL性能分析工具profile的使用(1)
- hadoop1.0的伪分布安装
- Application.OpenURL不同平台的使用
- [算法] 胜者树实现
- 重装系统时断电或强制关机导致硬盘故障的解决
- 分享一个ip地址库,有免费的
- win7系统安装软件时IE提示禁止安装无效签名怎么办?
- 托业考试要准备什么资料【zhasite】
- Spark学习笔记之-Spark Standalone(环境搭建)
- win7设置护眼模式
- 小马哥--高仿红米note 主板型号m8207 日期版本为15年324,拆机主板图与开机图
- ClassLoader, JavaAgent, Aspectj Weaving一站式扫盲帖