-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOneExitStream55.java
More file actions
45 lines (42 loc) · 1.09 KB
/
OneExitStream55.java
File metadata and controls
45 lines (42 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package offer;
/*
* 数据流中第一个出现的字符
*/
public class OneExitStream55 {
//Insert one char from stringstream
private int index = 0;
private int [] ouccrence = new int [256];
public OneExitStream55() {
// TODO Auto-generated constructor stub
for (int i = 0; i < ouccrence.length; i++) {
ouccrence[i] = -1;
}
}
public void Insert(char ch)
{
if(ch > 255) {
throw new IllegalArgumentException(ch + " ASCII");
}
if(ouccrence[ch] == -1){
ouccrence[ch] = index;
}else{
ouccrence[ch] = -2;
}
index++;
}
//return the first appearence once char in current stringstream
public char FirstAppearingOnce()
{
char ch = '\0';
int minIndex = Integer.MAX_VALUE;
for(int i=0;i<256;i++){
if(ouccrence[i] >=0 && ouccrence[i]<minIndex){
ch = (char)i;
minIndex = ouccrence[i];
}
}
if(ch == '\0')
return '#';
return ch;
}
}