字符流中第一个不重复的字符
2016-03-09 15:42
197 查看
直接代码:
package TestProblem;
public class Test55 {
/**
* 题目:请实现一个函数用来找出字符流中第一个只出现一次的字符。
*/
private static class CharStatistics{
//出现一次的标识
private int index=0;
private int[] occurrence = new int[256];
public CharStatistics(){
for (int i = 0; i < occurrence.length; i++) {
occurrence[i]=-1; //默认初始全为-1
}
}
private void Insert(char ch){
if(ch>255){
throw new IllegalArgumentException(ch+"must be a ASCII char");
}
//只出现一次
if(occurrence[ch]==-1){
occurrence[ch]=index;//index记录字符串中的位置
}else{
//出现两次
occurrence[ch]=-2;
}
index++;
}
public char firstAppearingOnce(String data){
if (data == null) {
throw new IllegalArgumentException(data);
}
for (int i = 0; i < data.length(); i++) {
Insert(data.charAt(i));
}
//在对应的哈希表完了之后进行统计
char ch='\0';
//用于记录最小的索引,对应的就是第一个不重复的数字
int minIndex=Integer.MAX_VALUE;
for (int i = 0; i < occurrence.length; i++) {//遍历一遍数组
if(occurrence[i]>=0 &&occurrence[i]<minIndex){
ch=(char)i;
minIndex=occurrence[i];//找到最小的值主要是minIndex在不断的更新
}
}
return ch;
}
}
public static void main(String[] args) {
// System.out.println(new CharStatistics().firstAppearingOnce("")); // '\0'
// System.out.println(new CharStatistics().firstAppearingOnce("g")); // 'g'
// System.out.println(new CharStatistics().firstAppearingOnce("go")); // 'g'
// System.out.println(new CharStatistics().firstAppearingOnce("goo")); // 'g'
// System.out.println(new CharStatistics().firstAppearingOnce("goog")); // '\0'
// System.out.println(new CharStatistics().firstAppearingOnce("googl")); // l
System.out.println(new CharStatistics().firstAppearingOnce("google")); // l
}
}
package TestProblem;
public class Test55 {
/**
* 题目:请实现一个函数用来找出字符流中第一个只出现一次的字符。
*/
private static class CharStatistics{
//出现一次的标识
private int index=0;
private int[] occurrence = new int[256];
public CharStatistics(){
for (int i = 0; i < occurrence.length; i++) {
occurrence[i]=-1; //默认初始全为-1
}
}
private void Insert(char ch){
if(ch>255){
throw new IllegalArgumentException(ch+"must be a ASCII char");
}
//只出现一次
if(occurrence[ch]==-1){
occurrence[ch]=index;//index记录字符串中的位置
}else{
//出现两次
occurrence[ch]=-2;
}
index++;
}
public char firstAppearingOnce(String data){
if (data == null) {
throw new IllegalArgumentException(data);
}
for (int i = 0; i < data.length(); i++) {
Insert(data.charAt(i));
}
//在对应的哈希表完了之后进行统计
char ch='\0';
//用于记录最小的索引,对应的就是第一个不重复的数字
int minIndex=Integer.MAX_VALUE;
for (int i = 0; i < occurrence.length; i++) {//遍历一遍数组
if(occurrence[i]>=0 &&occurrence[i]<minIndex){
ch=(char)i;
minIndex=occurrence[i];//找到最小的值主要是minIndex在不断的更新
}
}
return ch;
}
}
public static void main(String[] args) {
// System.out.println(new CharStatistics().firstAppearingOnce("")); // '\0'
// System.out.println(new CharStatistics().firstAppearingOnce("g")); // 'g'
// System.out.println(new CharStatistics().firstAppearingOnce("go")); // 'g'
// System.out.println(new CharStatistics().firstAppearingOnce("goo")); // 'g'
// System.out.println(new CharStatistics().firstAppearingOnce("goog")); // '\0'
// System.out.println(new CharStatistics().firstAppearingOnce("googl")); // l
System.out.println(new CharStatistics().firstAppearingOnce("google")); // l
}
}
相关文章推荐
- Nginx支持php配置
- .net面试总结
- 作业一:(2)课程进度总结
- document.getElementsByTagName
- 从setContentView分析Android加载布局的流程
- iOS多线程开发
- vector resize reserve区别
- 使用Unity发布第一个程序时遇到的问题
- Tcp
- C#第七节课
- Oracle 截取字符串,取系统时间
- 协议(protocol)与网络协议
- Android 6.0 新特性(官方文档翻译)
- php session过期时间设置,纯新手教学
- 第一场
- MSMQ 跨服务器读写队列的“消息队列系统的访问被拒绝”的解决方案
- 表示数值的字符串
- Makefile学习笔记(一):Windows平台(转载)
- Collection 和 Collections的区别
- 2016年3月